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

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';

// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Use the admin database for the operation
const adminDb = client.db(dbName).admin();

// List all the available databases
adminDb.listDatabases(function(err, dbs) {
test.equal(null, err);
test.ok(dbs.databases.length > 0);
client.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 4.X
or higher stream

AGGREGATIONCURSOR Cannot directly be instantiated

Example

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Create a collection we want to drop later
const col = client.db(dbName).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);
client.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

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Create a collection we want to drop later
const col = client.db(dbName).collection('createIndexExample1');
// Show that duplicate records got dropped
col.find({}).toArray(function(err, items) {
test.equal(null, err);
test.equal(4, items.length);
client.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

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Create a collection we want to drop later
const col = client.db(dbName).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);
client.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 4.X
or higher stream

CURSORS Cannot directly be instantiated

Example

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Create a collection we want to drop later
const col = client.db(dbName).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);
client.close();
});
});
});

lib/db.js

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

Example

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Get an additional db
const testDb = client.db('test');
client.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

const MongoClient = require('mongodb').MongoClient;
const GridStore = require('mongodb').GridStore;
const ObjectID = require('mongodb').ObjectID;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
const db = client.db(dbName);
const 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);
client.close();
test.done();
});
});
});
});
});

lib/mongo_client.js

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

Example

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
const db = client.db(dbName);
client.close();
});

lib/topologies/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

lib/topologies/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

lib/topologies/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

comments powered by Disqus