Index

lib/admin.js

The Admin class is an internal class that allows convenient access to
the admin functionality and commands for MongoDB.

ADMIN Cannot directly be instantiated

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Use the admin database for the operation
var adminDb = db.admin();

// List all the available databases
adminDb.listDatabases(function(err, dbs) {
test.equal(null, err);
test.ok(dbs.databases.length > 0);
db.close();
});
});

lib/aggregation_cursor.js

The AggregationCursor class is an internal class that embodies an aggregation cursor on MongoDB
allowing for iteration over the results returned from the underlying query. It supports
one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
or higher stream

AGGREGATIONCURSOR Cannot directly be instantiated

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Create a collection we want to drop later
var col = db.collection('createIndexExample1');
// Insert a bunch of documents
col.insert([{a:1, b:1}
, {a:2, b:2}, {a:3, b:3}
, {a:4, b:4}], {w:1}, function(err, result) {
test.equal(null, err);
// Show that duplicate records got dropped
col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
test.equal(null, err);
test.equal(4, items.length);
db.close();
});
});
});

lib/collection.js

The Collection class is an internal class that embodies a MongoDB collection
allowing for insert/update/remove/find and other command operation on that MongoDB collection.

COLLECTION Cannot directly be instantiated

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Create a collection we want to drop later
var col = db.collection('createIndexExample1');
// Show that duplicate records got dropped
col.find({}).toArray(function(err, items) {
test.equal(null, err);
test.equal(4, items.length);
db.close();
});
});

lib/command_cursor.js

The CommandCursor class is an internal class that embodies a
generalized cursor based on a MongoDB command allowing for iteration over the
results returned. It supports one by one document iteration, conversion to an
array or can be iterated as a Node 0.10.X or higher stream

CommandCursor Cannot directly be instantiated

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Create a collection we want to drop later
var col = db.collection('listCollectionsExample1');
// Insert a bunch of documents
col.insert([{a:1, b:1}
, {a:2, b:2}, {a:3, b:3}
, {a:4, b:4}], {w:1}, function(err, result) {
test.equal(null, err);

// List the database collections available
db.listCollections().toArray(function(err, items) {
  test.equal(null, err);
  db.close();
});

});
});

lib/cursor.js

The Cursor class is an internal class that embodies a cursor on MongoDB
allowing for iteration over the results returned from the underlying query. It supports
one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
or higher stream

CURSORS Cannot directly be instantiated

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Create a collection we want to drop later
var col = db.collection('createIndexExample1');
// Insert a bunch of documents
col.insert([{a:1, b:1}
, {a:2, b:2}, {a:3, b:3}
, {a:4, b:4}], {w:1}, function(err, result) {
test.equal(null, err);

// Show that duplicate records got dropped
col.find({}).toArray(function(err, items) {
  test.equal(null, err);
  test.equal(4, items.length);
  db.close();
});

});
});

lib/db.js

The Db class is a class that represents a MongoDB Database.

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Get an additional db
var testDb = db.db('test');
db.close();
});

lib/gridfs/grid_store.js

GridFS is a tool for MongoDB to store files to the database.
Because of the restrictions of the object size the database can hold, a
facility to split a file into several chunks is needed. The GridStore
class offers a simplified api to interact with files while managing the
chunks of split files behind the scenes. More information about GridFS can be
found here.

Example

var MongoClient = require('mongodb').MongoClient,
GridStore = require('mongodb').GridStore,
ObjectID = require('mongodb').ObjectID,
test = require('assert');

// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
var gridStore = new GridStore(db, null, "w");
gridStore.open(function(err, gridStore) {
gridStore.write("hello world!", function(err, gridStore) {
gridStore.close(function(err, result) {

    // Let's read the file using object Id
    GridStore.read(db, result._id, function(err, data) {
      test.equal('hello world!', data);
      db.close();
      test.done();
    });
  });
});

});
});

lib/mongo_client.js

The MongoClient class is a class that allows for making Connections to MongoDB.

Example

var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Get an additional db
db.close();
});

lib/mongos.js

The Mongos class is a class that represents a Mongos Proxy topology and is
used to construct connections.

Mongos Should not be used, use MongoClient.connect

Example

var Db = require('mongodb').Db,
Mongos = require('mongodb').Mongos,
Server = require('mongodb').Server,
test = require('assert');
// Connect using Mongos
var server = new Server('localhost', 27017);
var db = new Db('test', new Mongos([server]));
db.open(function(err, db) {
// Get an additional db
db.close();
});

lib/read_preference.js

The ReadPreference class is a class that represents a MongoDB ReadPreference and is
used to construct connections.

Example

var Db = require('mongodb').Db,
ReplSet = require('mongodb').ReplSet,
Server = require('mongodb').Server,
ReadPreference = require('mongodb').ReadPreference,
test = require('assert');
// Connect using ReplSet
var server = new Server('localhost', 27017);
var db = new Db('test', new ReplSet([server]));
db.open(function(err, db) {
test.equal(null, err);
// Perform a read
var cursor = db.collection('t').find({});
cursor.setReadPreference(ReadPreference.PRIMARY);
cursor.toArray(function(err, docs) {
test.equal(null, err);
db.close();
});
});

lib/replset.js

The ReplSet class is a class that represents a Replicaset topology and is
used to construct connections.

ReplSet Should not be used, use MongoClient.connect

Example

var Db = require('mongodb').Db,
ReplSet = require('mongodb').ReplSet,
Server = require('mongodb').Server,
test = require('assert');
// Connect using ReplSet
var server = new Server('localhost', 27017);
var db = new Db('test', new ReplSet([server]));
db.open(function(err, db) {
// Get an additional db
db.close();
});

lib/server.js

The Server class is a class that represents a single server topology and is
used to construct connections.

Server Should not be used, use MongoClient.connect

Example

var Db = require('mongodb').Db,
Server = require('mongodb').Server,
test = require('assert');
// Connect using single Server
var db = new Db('test', new Server('localhost', 27017););
db.open(function(err, db) {
// Get an additional db
db.close();
});