Create a new Collection instance (INTERNAL TYPE, do not instantiate directly)
- class Collection()¶
Arguments:
- db (object) – db instance.
- collectionName (string) – collection name.
- [pkFactory] (object) – alternative primary key factory.
- [options] (object) – additional options for the collection.
Returns: object a collection instance.
Inserts a single document or a an array of documents into MongoDB.
Arguments: |
|
---|---|
Returns: | collection |
Examples
A simple document insert example, not using safe mode to ensure document persistance on MongoDB
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Fetch a collection to insert document into db.open(function(err, db) { var collection = db.collection("simple_document_insert_collection_no_safe"); // Insert a single document collection.insert({hello:'world_no_safe'}); // Wait for a second before finishing up, to ensure we have written the item to disk setTimeout(function() { // Fetch the document collection.findOne({hello:'world_no_safe'}, function(err, item) { assert.equal(null, err); assert.equal('world_no_safe', item.hello); db.close(); }) }, 100); });A batch document insert example, using safe mode to ensure document persistance on MongoDB
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); db.open(function(err, db) { // Fetch a collection to insert document into var collection = db.collection("batch_document_insert_collection_safe"); // Insert a single document collection.insert([{hello:'world_safe1'} , {hello:'world_safe2'}], {w:1}, function(err, result) { assert.equal(null, err); // Fetch the document collection.findOne({hello:'world_safe2'}, function(err, item) { assert.equal(null, err); assert.equal('world_safe2', item.hello); db.close(); }) }); });Example of inserting a document containing functions
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); db.open(function(err, db) { // Fetch a collection to insert document into var collection = db.collection("simple_document_insert_with_function_safe"); // Insert a single document collection.insert({hello:'world' , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) { assert.equal(null, err); // Fetch the document collection.findOne({hello:'world'}, function(err, item) { assert.equal(null, err); assert.ok("function() {}", item.code); db.close(); }) }); });Example of using keepGoing to allow batch insert to complete even when there are illegal documents in the batch
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Only run the rest of the code if we have a mongodb server with version >= 1.9.1 db.open(function(err, db) { // Create a collection var collection = db.collection('keepGoingExample'); // Add an unique index to title to force errors in the batch insert collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) { // Insert some intial data into the collection collection.insert([{name:"Jim"} , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) { // Force keep going flag, ignoring unique index issue collection.insert([{name:"Jim"} , {name:"Sarah", title:"Princess"} , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) { // Count the number of documents left (should not include the duplicates) collection.count(function(err, count) { assert.equal(3, count); }) }); }); }); });
Removes documents specified by <code>selector</code> from the db.
Arguments: |
|
---|---|
Returns: | null |
Examples
An example removing all documents in a collection not using safe mode
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Fetch a collection to insert document into db.collection("remove_all_documents_no_safe", function(err, collection) { // Insert a bunch of documents collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) { assert.equal(null, err); // Remove all the document collection.remove(); // Fetch all results collection.find().toArray(function(err, items) { assert.equal(null, err); assert.equal(0, items.length); db.close(); }); }); }) });An example removing a subset of documents using safe mode to ensure removal of documents
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Fetch a collection to insert document into db.collection("remove_subset_of_documents_safe", function(err, collection) { // Insert a bunch of documents collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) { assert.equal(null, err); // Remove all the document collection.remove({a:1}, {w:1}, function(err, numberOfRemovedDocs) { assert.equal(null, err); assert.equal(1, numberOfRemovedDocs); db.close(); }); }); }) });
Renames the collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
An example of illegal and legal renaming of a collection
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Open a couple of collections db.createCollection('test_rename_collection', function(err, collection1) { db.createCollection('test_rename_collection2', function(err, collection2) { // Attemp to rename a collection to a number try { collection1.rename(5, function(err, collection) {}); } catch(err) { assert.ok(err instanceof Error); assert.equal("collection name must be a String", err.message); } // Attemp to rename a collection to an empty string try { collection1.rename("", function(err, collection) {}); } catch(err) { assert.ok(err instanceof Error); assert.equal("collection names cannot be empty", err.message); } // Attemp to rename a collection to an illegal name including the character $ try { collection1.rename("te$t", function(err, collection) {}); } catch(err) { assert.ok(err instanceof Error); assert.equal("collection names must not contain '$'", err.message); } // Attemp to rename a collection to an illegal name starting with the character . try { collection1.rename(".test", function(err, collection) {}); } catch(err) { assert.ok(err instanceof Error); assert.equal("collection names must not start or end with '.'", err.message); } // Attemp to rename a collection to an illegal name ending with the character . try { collection1.rename("test.", function(err, collection) {}); } catch(err) { assert.ok(err instanceof Error); assert.equal("collection names must not start or end with '.'", err.message); } // Attemp to rename a collection to an illegal name with an empty middle name try { collection1.rename("tes..t", function(err, collection) {}); } catch(err) { assert.equal("collection names cannot be empty", err.message); } // Insert a couple of documents collection1.insert([{'x':1}, {'x':2}], {w:1}, function(err, docs) { // Attemp to rename the first collection to the second one, this will fail collection1.rename('test_rename_collection2', function(err, collection) { assert.ok(err instanceof Error); assert.ok(err.message.length > 0); // Attemp to rename the first collection to a name that does not exist // this will be succesful collection1.rename('test_rename_collection3', function(err, collection) { assert.equal("test_rename_collection3", collection.collectionName); // Ensure that the collection is pointing to the new one collection1.count(function(err, count) { assert.equal(2, count); db.close(); }); }); }); }) }); }); });
Save a document. Simple full document replacement function. Not recommended for efficiency, use atomic operators and update instead for more efficient operations.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of a simple document save with safe set to false
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Fetch the collection var collection = db.collection("save_a_simple_document"); // Save a document with no safe option collection.save({hello:'world'}); // Wait for a second setTimeout(function() { // Find the saved document collection.findOne({hello:'world'}, function(err, item) { assert.equal(null, err); assert.equal('world', item.hello); db.close(); }); }, 1000); });Example of a simple document save and then resave with safe set to true
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Fetch the collection var collection = db.collection("save_a_simple_document_modify_it_and_resave_it"); // Save a document with no safe option collection.save({hello:'world'}, {w: 0}, function(err, result) { // Find the saved document collection.findOne({hello:'world'}, function(err, item) { assert.equal(null, err); assert.equal('world', item.hello); // Update the document item['hello2'] = 'world2'; // Save the item with the additional field collection.save(item, {w: 1}, function(err, result) { // Find the changed document collection.findOne({hello:'world'}, function(err, item) { assert.equal(null, err); assert.equal('world', item.hello); assert.equal('world2', item.hello2); db.close(); }); }); }); }); });
Updates documents.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of a simple document update with safe set to false on an existing document
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Get a collection db.collection('update_a_simple_document', function(err, collection) { // Insert a document, then update it collection.insert({a:1}, {w: 1}, function(err, doc) { // Update the document with an atomic operator collection.update({a:1}, {$set:{b:2}}); // Wait for a second then fetch the document setTimeout(function() { // Fetch the document that we modified collection.findOne({a:1}, function(err, item) { assert.equal(null, err); assert.equal(1, item.a); assert.equal(2, item.b); db.close(); }); }, 1000); }) }); });Example of a simple document update using upsert (the document will be inserted if it does not exist)
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Get a collection db.collection('update_a_simple_document_upsert', function(err, collection) { // Update the document using an upsert operation, ensuring creation if it does not exist collection.update({a:1}, {b:2, a:1}, {upsert:true, w: 1}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Fetch the document that we modified and check if it got inserted correctly collection.findOne({a:1}, function(err, item) { assert.equal(null, err); assert.equal(1, item.a); assert.equal(2, item.b); db.close(); }); }); }); });Example of an update across multiple documents using the multi option.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Get a collection db.collection('update_a_simple_document_multi', function(err, collection) { // Insert a couple of documentations collection.insert([{a:1, b:1}, {a:1, b:2}], {w: 1}, function(err, result) { // Update multiple documents using the multi option collection.update({a:1}, {$set:{b:0}}, {w: 1, multi:true}, function(err, numberUpdated) { assert.equal(null, err); assert.equal(2, numberUpdated); // Fetch all the documents and verify that we have changed the b value collection.find().toArray(function(err, items) { assert.equal(null, err); assert.equal(1, items[0].a); assert.equal(0, items[0].b); assert.equal(1, items[1].a); assert.equal(0, items[1].b); db.close(); }); }) }); }); });
The distinct command returns returns a list of distinct values for the given key across a collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of running the distinct command against a collection
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Crete the collection for the distinct example db.createCollection('simple_key_based_distinct', function(err, collection) { // Insert documents to perform distinct against collection.insert([{a:0, b:{c:'a'}}, {a:1, b:{c:'b'}}, {a:1, b:{c:'c'}}, {a:2, b:{c:'a'}}, {a:3}, {a:3}], {w: 1}, function(err, ids) { // Peform a distinct query against the a field collection.distinct('a', function(err, docs) { assert.deepEqual([0, 1, 2, 3], docs.sort()); // Perform a distinct query against the sub-field b.c collection.distinct('b.c', function(err, docs) { assert.deepEqual(['a', 'b', 'c'], docs.sort()); db.close(); }); }); }) }); });Example of running the distinct command against a collection with a filter query
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Crete the collection for the distinct example db.createCollection('simple_key_based_distinct_sub_query_filter', function(err, collection) { // Insert documents to perform distinct against collection.insert([{a:0, b:{c:'a'}}, {a:1, b:{c:'b'}}, {a:1, b:{c:'c'}}, {a:2, b:{c:'a'}}, {a:3}, {a:3}, {a:5, c:1}], {w: 1}, function(err, ids) { // Peform a distinct query with a filter against the documents collection.distinct('a', {c:1}, function(err, docs) { assert.deepEqual([5], docs.sort()); db.close(); }); }) }); });
Count number of matching documents in the db to a query.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of running simple count commands against a collection.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Crete the collection for the distinct example db.createCollection('simple_count_example', function(err, collection) { // Insert documents to perform distinct against collection.insert([{a:1}, {a:2}, {a:3}, {a:4, b:1}], {w: 1}, function(err, ids) { // Perform a total count command collection.count(function(err, count) { assert.equal(null, err); assert.equal(4, count); // Peform a partial account where b=1 collection.count({b:1}, function(err, count) { assert.equal(null, err); assert.equal(1, count); db.close(); }); }); }); }); });
Drop the collection
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of a simple document save and then resave with safe set to true
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('test_other_drop', function(err, collection) { assert.equal(null, err); // Drop the collection collection.drop(function(err, reply) { // Ensure we don't have the collection in the set of names db.collectionNames(function(err, replies) { var found = false; // For each collection in the list of collection names in this db look for the // dropped collection replies.forEach(function(document) { if(document.name == "test_other_drop") { found = true; return; } }); // Ensure the collection is not found assert.equal(false, found); // Let's close the db db.close(); }); }); }); });
Find and update a document.
Arguments: |
|
---|---|
Returns: | null |
Examples
A whole set of different ways to use the findAndModify command.
The first findAndModify command modifies a document and returns the modified document back. The second findAndModify command removes the document. The second findAndModify command upserts a document and returns the new document.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_find_and_modify_operations_', function(err, collection) { assert.equal(null, err); // Insert some test documentations collection.insert([{a:1}, {b:1}, {c:1}], {w:1}, function(err, result) { assert.equal(null, err); // Simple findAndModify command returning the new document collection.findAndModify({a:1}, [['a', 1]], {$set:{b1:1}}, {new:true}, function(err, doc) { assert.equal(null, err); assert.equal(1, doc.a); assert.equal(1, doc.b1); // Simple findAndModify command returning the new document and // removing it at the same time collection.findAndModify({b:1}, [['b', 1]], {$set:{b:2}}, {remove:true}, function(err, doc) { // Verify that the document is gone collection.findOne({b:1}, function(err, item) { assert.equal(null, err); assert.equal(null, item); // Simple findAndModify command performing an upsert and returning the new document // executing the command safely collection.findAndModify({d:1}, [['b', 1]], {d:1, f:1}, {new:true, upsert:true, w:1}, function(err, doc) { assert.equal(null, err); assert.equal(1, doc.d); assert.equal(1, doc.f); db.close(); }) }); }); }); }); }); });
Find and remove a document
Arguments: |
|
---|---|
Returns: | null |
Examples
An example of using findAndRemove
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_find_and_modify_operations_2', function(err, collection) { assert.equal(null, err); // Insert some test documentations collection.insert([{a:1}, {b:1, d:1}, {c:1}], {w:1}, function(err, result) { assert.equal(null, err); // Simple findAndModify command returning the old document and // removing it at the same time collection.findAndRemove({b:1}, [['b', 1]], function(err, doc) { assert.equal(null, err); assert.equal(1, doc.b); assert.equal(1, doc.d); // Verify that the document is gone collection.findOne({b:1}, function(err, item) { assert.equal(null, err); assert.equal(null, item); db.close(); }); }); }); }); });
Creates a cursor for a query that can be used to iterate over results from MongoDB
Arguments: |
|
---|---|
Returns: | cursor returns a cursor to the query |
Examples
A simple query using the find method on the collection.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_query', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the testing collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, result) { assert.equal(null, err); // Peform a simple find and return all the documents collection.find().toArray(function(err, docs) { assert.equal(null, err); assert.equal(3, docs.length); db.close(); }); }); }); });A simple query showing the explain for a query
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_explain_query', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the testing collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, result) { assert.equal(null, err); // Peform a simple find and return all the documents collection.find({}, {explain:true}).toArray(function(err, docs) { assert.equal(null, err); assert.equal(1, docs.length); db.close(); }); }); }); });A simple query showing skip and limit
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_limit_skip_query', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the testing collection.insert([{a:1, b:1}, {a:2, b:2}, {a:3, b:3}], {w:1}, function(err, result) { assert.equal(null, err); // Peform a simple find and return all the documents collection.find({}, {skip:1, limit:1, fields:{b:1}}).toArray(function(err, docs) { assert.equal(null, err); assert.equal(1, docs.length); assert.equal(null, docs[0].a); assert.equal(2, docs[0].b); db.close(); }); }); }); });
Finds a single document based on the query
Arguments: |
|
---|---|
Returns: | cursor returns a cursor to the query |
Examples
A simple query using findOne
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_limit_skip_find_one_query', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the testing collection.insert([{a:1, b:1}, {a:2, b:2}, {a:3, b:3}], {w:1}, function(err, result) { assert.equal(null, err); // Peform a simple find and return all the documents collection.findOne({a:2}, {fields:{b:1}}, function(err, doc) { assert.equal(null, err); assert.equal(null, doc.a); assert.equal(2, doc.b); db.close(); }); }); }); });
Creates an index on the collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple createIndex using a simple single field index
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('simple_index_test', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the index collection.insert([{a:1}, {a:2}, {a:3}, {a:4}], {w:1}, function(err, result) { assert.equal(null, err); // Create an index on the a field collection.createIndex('a', {w:1}, function(err, indexName) { assert.equal("a_1", indexName); // Peform a query, with explain to show we hit the query collection.find({a:2}, {explain:true}).toArray(function(err, explanation) { assert.deepEqual([[2, 2]], explanation[0].indexBounds.a); db.close(); }); }); }); }); });A more complex createIndex using a compound unique index in the background and dropping duplicated documents
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('more_complex_index_test', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the index collection.insert([{a:1, b:1}, {a:1, b:1} , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { assert.equal(null, err); var options = {unique:true, background:true, dropDups:true, w:1}; // Create an index on the a field collection.createIndex({a:1, b:1} , options, function(err, indexName) { assert.ok(!options.readPreference); // Show that duplicate records got dropped collection.find({}).toArray(function(err, items) { assert.equal(null, err); assert.equal(4, items.length); // Peform a query, with explain to show we hit the query collection.find({a:2}, {explain:true}).toArray(function(err, explanation) { assert.equal(null, err); assert.ok(explanation[0].indexBounds.a != null); assert.ok(explanation[0].indexBounds.b != null); db.close(); }); }) }); }); }); });
Ensures that an index exists, if it does not it creates it
Arguments: |
|
---|---|
Returns: | null |
Examples
A more complex ensureIndex using a compound unique index in the background and dropping duplicated documents.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('more_complex_ensure_index_test', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the index collection.insert([{a:1, b:1}, {a:1, b:1} , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { assert.equal(null, err); // Create an index on the a field collection.ensureIndex({a:1, b:1} , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { // Show that duplicate records got dropped collection.find({}).toArray(function(err, items) { assert.equal(null, err); assert.equal(4, items.length); // Peform a query, with explain to show we hit the query collection.find({a:2}, {explain:true}).toArray(function(err, explanation) { assert.equal(null, err); assert.ok(explanation[0].indexBounds.a != null); assert.ok(explanation[0].indexBounds.b != null); db.close(); }); }) }); }); }); });
Retrieves this collections index info.
Arguments: |
|
---|---|
Returns: | null |
Examples
An examples showing the information returned by indexInformation
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('more_index_information_test', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the index collection.insert([{a:1, b:1}, {a:1, b:1} , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { assert.equal(null, err); // Create an index on the a field collection.ensureIndex({a:1, b:1} , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { // Fetch basic indexInformation for collection collection.indexInformation(function(err, indexInformation) { assert.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); assert.deepEqual([ [ 'a', 1 ], [ 'b', 1 ] ], indexInformation.a_1_b_1); // Fetch full index information collection.indexInformation({full:true}, function(err, indexInformation) { assert.deepEqual({ _id: 1 }, indexInformation[0].key); assert.deepEqual({ a: 1, b: 1 }, indexInformation[1].key); db.close(); }); }); }); }); }); });
Drops an index from this collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
An examples showing the creation and dropping of an index
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('create_and_drop_an_index', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the index collection.insert([{a:1, b:1}, {a:1, b:1} , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { assert.equal(null, err); // Create an index on the a field collection.ensureIndex({a:1, b:1} , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { // Drop the index collection.dropIndex("a_1_b_1", function(err, result) { assert.equal(null, err); // Verify that the index is gone collection.indexInformation(function(err, indexInformation) { assert.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); assert.equal(null, indexInformation.a_1_b_1); db.close(); }); }); }); }); }); });
Drops all indexes from this collection.
Arguments: |
|
---|---|
Returns: | null |
Reindex all indexes on the collection Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
Arguments: |
|
---|---|
Returns: | null |
Examples
An example showing how to force a reindex of a collection.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection we want to drop later db.createCollection('shouldCorrectlyForceReindexOnCollection', function(err, collection) { assert.equal(null, err); // Insert a bunch of documents for the index collection.insert([{a:1, b:1}, {a:1, b:1} , {a:2, b:2}, {a:3, b:3}, {a:4, b:4, c:4}], {w:1}, function(err, result) { assert.equal(null, err); // Create an index on the a field collection.ensureIndex({a:1, b:1} , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { // Force a reindex of the collection collection.reIndex(function(err, result) { assert.equal(null, err); assert.equal(true, result); // Verify that the index is gone collection.indexInformation(function(err, indexInformation) { assert.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); assert.deepEqual([ [ 'a', 1 ], [ 'b', 1 ] ], indexInformation.a_1_b_1); db.close(); }); }); }); }); }); });
Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple map reduce example
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection db.createCollection('test_map_reduce_functions', function(err, collection) { // Insert some documents to perform map reduce over collection.insert([{'user_id':1}, {'user_id':2}], {w:1}, function(err, r) { // Map function var map = function() { emit(this.user_id, 1); }; // Reduce function var reduce = function(k,vals) { return 1; }; // Peform the map reduce collection.mapReduce(map, reduce, {out: {replace : 'tempCollection', readPreference : 'secondary'}}, function(err, collection) { // Mapreduce returns the temporary collection with the results collection.findOne({'_id':1}, function(err, result) { assert.equal(1, result.value); collection.findOne({'_id':2}, function(err, result) { assert.equal(1, result.value); db.close(); }); }); }); }); }); });A simple map reduce example using the inline output type on MongoDB > 1.7.6 returning the statistics
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection db.createCollection('test_map_reduce_functions_inline', function(err, collection) { // Insert some test documents collection.insert([{'user_id':1}, {'user_id':2}], {w:1}, function(err, r) { // Map function var map = function() { emit(this.user_id, 1); }; // Reduce function var reduce = function(k,vals) { return 1; }; // Execute map reduce and return results inline collection.mapReduce(map, reduce, {out : {inline: 1}, verbose:true}, function(err, results, stats) { assert.equal(2, results.length); assert.ok(stats != null); collection.mapReduce(map, reduce, {out : {replace: 'mapreduce_integration_test'}, verbose:true}, function(err, results, stats) { assert.ok(stats != null); db.close(); }); }); }); }); });Mapreduce different test with a provided scope containing a javascript function.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection db.createCollection('test_map_reduce_functions_scope', function(err, collection) { // Insert some test documents collection.insert([{'user_id':1, 'timestamp':new Date()} , {'user_id':2, 'timestamp':new Date()}], {w:1}, function(err, r) { // Map function var map = function(){ emit(fn(this.timestamp.getYear()), 1); } // Reduce function var reduce = function(k, v){ count = 0; for(i = 0; i < v.length; i++) { count += v[i]; } return count; } // Javascript function available in the map reduce scope var t = function(val){ return val+1; } // Execute the map reduce with the custom scope var o = {}; o.scope = { fn: new Code(t.toString()) } o.out = { replace: 'replacethiscollection' } collection.mapReduce(map, reduce, o, function(err, outCollection) { assert.equal(null, err); // Find all entries in the map-reduce collection outCollection.find().toArray(function(err, results) { assert.equal(null, err); assert.equal(2, results[0].value) // mapReduce with scope containing plain function var o = {}; o.scope = { fn: t } o.out = { replace: 'replacethiscollection' } collection.mapReduce(map, reduce, o, function(err, outCollection) { assert.equal(null, err); // Find all entries in the map-reduce collection outCollection.find().toArray(function(err, results) { assert.equal(2, results[0].value) db.close(); }); }); }); }); }); }); });Mapreduce different test with a provided scope containing javascript objects with functions.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection db.createCollection('test_map_reduce_functions_scope_objects', function(err, collection) { // Insert some test documents collection.insert([{'user_id':1, 'timestamp':new Date()} , {'user_id':2, 'timestamp':new Date()}], {w:1}, function(err, r) { // Map function var map = function(){ emit(obj.fn(this.timestamp.getYear()), 1); } // Reduce function var reduce = function(k, v){ count = 0; for(i = 0; i < v.length; i++) { count += v[i]; } return count; } // Javascript function available in the map reduce scope var t = function(val){ return val+1; } // Execute the map reduce with the custom scope containing objects var o = {}; o.scope = { obj: {fn: new Code(t.toString())} } o.out = { replace: 'replacethiscollection' } collection.mapReduce(map, reduce, o, function(err, outCollection) { assert.equal(null, err); // Find all entries in the map-reduce collection outCollection.find().toArray(function(err, results) { assert.equal(null, err); assert.equal(2, results[0].value) // mapReduce with scope containing plain function var o = {}; o.scope = { obj: {fn: t} } o.out = { replace: 'replacethiscollection' } collection.mapReduce(map, reduce, o, function(err, outCollection) { assert.equal(null, err); // Find all entries in the map-reduce collection outCollection.find().toArray(function(err, results) { assert.equal(2, results[0].value) db.close(); }); }); }); }); }); }); });
Run a group command across a collection
Arguments: |
|
---|---|
Returns: | null |
Examples
A whole lot of different wayt to execute the group command
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection db.createCollection('test_group', function(err, collection) { // Peform a simple group by on an empty collection collection.group([], {}, {"count":0}, "function (obj, prev) { prev.count++; }", function(err, results) { assert.deepEqual([], results); // Trigger some inserts on the collection collection.insert([{'a':2}, {'b':5}, {'a':1}], {w:1}, function(err, ids) { // Perform a group count collection.group([], {}, {"count":0}, "function (obj, prev) { prev.count++; }", function(err, results) { assert.equal(3, results[0].count); // Pefrom a group count using the eval method collection.group([], {}, {"count":0}, "function (obj, prev) { prev.count++; }", false, function(err, results) { assert.equal(3, results[0].count); // Group with a conditional collection.group([], {'a':{'$gt':1}}, {"count":0}, "function (obj, prev) { prev.count++; }", function(err, results) { // Results assert.equal(1, results[0].count); // Group with a conditional using the EVAL method collection.group([], {'a':{'$gt':1}}, {"count":0}, "function (obj, prev) { prev.count++; }" , false, function(err, results) { // Results assert.equal(1, results[0].count); // Insert some more test data collection.insert([{'a':2}, {'b':3}], {w:1}, function(err, ids) { // Do a Group by field a collection.group(['a'], {}, {"count":0}, "function (obj, prev) { prev.count++; }", function(err, results) { // Results assert.equal(2, results[0].a); assert.equal(2, results[0].count); assert.equal(null, results[1].a); assert.equal(2, results[1].count); assert.equal(1, results[2].a); assert.equal(1, results[2].count); // Do a Group by field a collection.group({'a':true}, {}, {"count":0}, function (obj, prev) { prev.count++; }, true, function(err, results) { // Results assert.equal(2, results[0].a); assert.equal(2, results[0].count); assert.equal(null, results[1].a); assert.equal(2, results[1].count); assert.equal(1, results[2].a); assert.equal(1, results[2].count); // Correctly handle illegal function collection.group([], {}, {}, "5 ++ 5", function(err, results) { assert.ok(err.message != null); // Use a function to select the keys used to group by var keyf = function(doc) { return {a: doc.a}; }; collection.group(keyf, {a: {$gt: 0}}, {"count": 0, "value": 0}, function(obj, prev) { prev.count++; prev.value += obj.a; }, true, function(err, results) { // Results results.sort(function(a, b) { return b.count - a.count; }); assert.equal(2, results[0].count); assert.equal(2, results[0].a); assert.equal(4, results[0].value); assert.equal(1, results[1].count); assert.equal(1, results[1].a); assert.equal(1, results[1].value); // Use a Code object to select the keys used to group by var keyf = new Code(function(doc) { return {a: doc.a}; }); collection.group(keyf, {a: {$gt: 0}}, {"count": 0, "value": 0}, function(obj, prev) { prev.count++; prev.value += obj.a; }, true, function(err, results) { // Results results.sort(function(a, b) { return b.count - a.count; }); assert.equal(2, results[0].count); assert.equal(2, results[0].a); assert.equal(4, results[0].value); assert.equal(1, results[1].count); assert.equal(1, results[1].a); assert.equal(1, results[1].value); // Correctly handle illegal function when using the EVAL method collection.group([], {}, {}, "5 ++ 5", false, function(err, results) { assert.ok(err.message != null); db.close(); }); }); }); }); }); }); }); }); }); }); }); }); }); }); });
Returns the options of the collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
An example returning the options for a collection.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection that we are getting the options back from db.createCollection('test_collection_options', {'capped':true, 'size':1024}, function(err, collection) { assert.ok(collection instanceof Collection); assert.equal('test_collection_options', collection.collectionName); // Let's fetch the collection options collection.options(function(err, options) { assert.equal(true, options.capped); assert.ok(options.size >= 1024); db.close(); }); }); });
Returns if the collection is a capped collection
Arguments: |
|
---|---|
Returns: | null |
Examples
An example showing how to establish if it’s a capped collection
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection that we are getting the options back from db.createCollection('test_collection_is_capped', {'capped':true, 'size':1024}, function(err, collection) { assert.ok(collection instanceof Collection); assert.equal('test_collection_is_capped', collection.collectionName); // Let's fetch the collection options collection.isCapped(function(err, capped) { assert.equal(true, capped); db.close(); }); }); });
Checks if one or more indexes exist on the collection
Arguments: |
|
---|---|
Returns: | null |
Examples
An example showing the use of the indexExists function for a single index name and a list of index names.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a test collection that we are getting the options back from db.createCollection('test_collection_index_exists', {w: 1}, function(err, collection) { assert.equal(null, err); // Create an index on the collection collection.createIndex('a', {w: 1}, function(err, indexName) { // Let's test to check if a single index exists collection.indexExists("a_1", function(err, result) { assert.equal(true, result); // Let's test to check if multiple indexes are available collection.indexExists(["a_1", "_id_"], function(err, result) { assert.equal(true, result); // Check if a non existing index exists collection.indexExists("c_1", function(err, result) { assert.equal(false, result); db.close(); }); }); }); }); }); });
Execute the geoNear command to search for items in the collection
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of a simple geoNear query across some documents
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = configuration.newDbInstance({w:0}, {poolSize:1}); // Establish connection to db db.open(function(err, db) { // Fetch the collection var collection = db.collection("simple_geo_near_command"); // Add a location based index collection.ensureIndex({loc:"2d"}, function(err, result) { // Save a new location tagged document collection.insert([{a:1, loc:[50, 30]}, {a:1, loc:[30, 50]}], {w:1}, function(err, result) { // Use geoNear command to find document collection.geoNear(50, 50, {query:{a:1}, num:1}, function(err, docs) { assert.equal(1, docs.results.length); db.close(); }); }); }); });
Execute a geo search using a geo haystack index on a collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of a simple geoHaystackSearch query across some documents
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Fetch the collection var collection = db.collection("simple_geo_haystack_command"); // Add a location based index collection.ensureIndex({loc: "geoHaystack", type: 1}, {bucketSize: 1}, function(err, result) { // Save a new location tagged document collection.insert([{a:1, loc:[50, 30]}, {a:1, loc:[30, 50]}], {w:1}, function(err, result) { // Use geoNear command to find document collection.geoHaystackSearch(50, 50, {search:{a:1}, limit:1, maxDistance:100}, function(err, docs) { assert.equal(1, docs.results.length); db.close(); }); }); }); });
Retrieve all the indexes on the collection.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of retrieving a collections indexes
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Crete the collection for the distinct example db.createCollection('simple_key_based_distinct', function(err, collection) { // Create a geo 2d index collection.ensureIndex({loc:"2d"}, {w: 1}, function(err, result) { assert.equal(null, err); // Create a simple single field index collection.ensureIndex({a:1}, {w: 1}, function(err, result) { assert.equal(null, err); // List all of the indexes on the collection collection.indexes(function(err, indexes) { assert.equal(3, indexes.length); db.close(); }); }) }) }); });
Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.2
Arguments: |
|
---|---|
Returns: | null |
Examples
Correctly call the aggregation framework using a pipeline in an Array.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyExecuteSimpleAggregationPipelineUsingArray'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as an Array collection.aggregate([ { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} ], function(err, result) { assert.equal(null, err); assert.equal('good', result[0]._id.tags); assert.deepEqual(['bob'], result[0].authors); assert.equal('fun', result[1]._id.tags); assert.deepEqual(['bob'], result[1].authors); db.close(); }); }); });Correctly call the aggregation framework using a pipeline expressed as an argument list.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyExecuteSimpleAggregationPipelineUsingArguments'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as function call parameters // instead of an Array. collection.aggregate( { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} , function(err, result) { assert.equal(null, err); assert.equal('good', result[0]._id.tags); assert.deepEqual(['bob'], result[0].authors); assert.equal('fun', result[1]._id.tags); assert.deepEqual(['bob'], result[1].authors); db.close(); }); }); });Correctly call the aggregation framework using a pipeline expressed as an argument list.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyExecuteSimpleAggregationPipelineUsingArguments'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as function call parameters // instead of an Array. collection.aggregate( { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} , function(err, result) { assert.equal(null, err); assert.equal('good', result[0]._id.tags); assert.deepEqual(['bob'], result[0].authors); assert.equal('fun', result[1]._id.tags); assert.deepEqual(['bob'], result[1].authors); db.close(); }); }); });Correctly call the aggregation framework to return a cursor
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as an Array var cursor = collection.aggregate([ { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} ], { cursor: {batchSize:100} }); // Iterate over all the items in the cursor cursor.get(function(err, results) { assert.equal(null, err); assert.equal('good', results[0]._id.tags); assert.deepEqual(['bob'], results[0].authors); assert.equal('fun', results[1]._id.tags); assert.deepEqual(['bob'], results[1].authors); db.close(); }); }); });Correctly call the aggregation framework to return a cursor and call explain
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as an Array var cursor = collection.aggregate([ { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} ], { cursor: {batchSize:100} }); // Iterate over all the items in the cursor cursor.explain(function(err, results) { assert.equal(null, err); assert.equal(4, results.length); db.close(); }); }); });Correctly call the aggregation framework to return a cursor with batchSize 1 and get the first result using next
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as an Array var cursor = collection.aggregate([ { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} ], { cursor: {batchSize:1} }); // Iterate over all the items in the cursor cursor.next(function(err, result) { assert.equal(null, err); assert.equal('good', result._id.tags); assert.deepEqual(['bob'], result.authors); db.close(); }); }); });Correctly call the aggregation framework and write the results to a new collection
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as an Array collection.aggregate([ { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} ], { out: "testingOutCollectionForAggregation" }, function(err, results) { assert.equal(null, err); assert.equal(0, results.length); db.close(); }); }); });Correctly use allowDiskUse when performing an aggregation
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Some docs for insertion var docs = [{ title : "this is my title", author : "bob", posted : new Date() , pageViews : 5, tags : [ "fun" , "good" , "fun" ], other : { foo : 5 }, comments : [ { author :"joe", text : "this is cool" }, { author :"sam", text : "this is bad" } ]}]; // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs collection.insert(docs, {w: 1}, function(err, result) { // Execute aggregate, notice the pipeline is expressed as an Array collection.aggregate([ { $project : { author : 1, tags : 1 }}, { $unwind : "$tags" }, { $group : { _id : {tags : "$tags"}, authors : { $addToSet : "$author" } }} ], { allowDiskUse: true }, function(err, results) { assert.equal(null, err); assert.equal('good', results[0]._id.tags); assert.deepEqual(['bob'], results[0].authors); assert.equal('fun', results[1]._id.tags); assert.deepEqual(['bob'], results[1].authors); db.close(); }); }); });
Get all the collection statistics.
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of retrieving a collections stats
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Crete the collection for the distinct example db.createCollection('collection_stats_test', function(err, collection) { // Insert some documents collection.insert([{a:1}, {hello:'world'}], {w: 1}, function(err, result) { // Retrieve the statistics for the collection collection.stats(function(err, stats) { assert.equal(2, stats.count); db.close(); }); }); }); });
Initiate a Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
Arguments: |
|
---|---|
Returns: | unorderedbulkoperation |
Initiate an In order bulk write operation, operations will be serially executed in the order they are added, creating a new operation for each switch in types.
Arguments: |
|
---|---|
Returns: | orderedbulkoperation |
Return N number of parallel cursors for a collection allowing parallel reading of entire collection. There are no ordering guarantees for returned results.
Arguments: |
|
---|---|
Returns: | orderedbulkoperation |
Examples
A parallelCollectionScan example
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { var docs = []; // Insert some documents for(var i = 0; i < 2000; i++) { docs.push({a:i}); } // Get the collection var collection = db.collection('parallelCollectionScan'); // Insert 1000 documents in a batch collection.insert(docs, function(err, result) { var results = []; var numCursors = 3; // Execute parallelCollectionScan command collection.parallelCollectionScan({numCursors:numCursors}, function(err, cursors) { assert.equal(null, err); assert.ok(cursors != null); assert.ok(cursors.length > 0); for(var i = 0; i < cursors.length; i++) { cursors[i].get(function(err, items) { assert.equal(err, null); // Add docs to results array results = results.concat(items); numCursors = numCursors - 1; // No more cursors let's ensure we got all results if(numCursors == 0) { assert.equal(docs.length, results.length); db.close(); } }); } }); }); });