// 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;
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Select the database by name
const testDb = client.db(dbName);
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.