See also:
Collection object is a pointer to a specific collection in the database. If you want to insert new records or query existing ones then you need to have a valid collection object.
NB Collection names can’t start or end with a period nor contain a dollar sign! (.tes$t is not allowed)
Collections can be created with createCollection
db.createCollection([[name[, options]], callback)
where name is the name of the collection, options a set of configuration parameters and callback is a callback function. db is the database object.
The first parameter for the callback is the error object (null if no error) and the second one is the pointer to the newly created collection. If strict mode is on and the table exists, the operation yields in error. With strict mode off (default) the function simple returns the pointer to the existing collection and does not truncate it.
db.createCollection("test", function(err, collection){
collection.insert({"test":"value"});
});
Several options can be passed to the createCollection function with options parameter.
* `raw` - driver returns documents as bson binary Buffer objects, `default:false`
Example of usage:
console.log("Collection name: "+collection.collectionName)
Collections can be listed with collectionNames
db.collectionNames(callback);
callback gets two parameters - an error object (if error occured) and an array of collection names as strings.
Collection names also include database name, so a collection named posts in a database blog will be listed as blog.posts.
Additionally there’s system collections which should not be altered without knowing exactly what you are doing, these sollections can be identified with system prefix. For example posts.system.indexes.
Example:
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
db.collectionNames(function(err, collections){
console.log(collections);
});
});
Collection objects can be listed with database method collections
db.collections(callback)
Where callback gets two parameters - an error object (if an error occured) and an array of collection objects.
Existing collections can be opened with collection
db.collection([[name[, options]], callback);
If strict mode is off, then a new collection is created if not already present.
Several options can be passed to the collection function with options parameter.
* `raw` - driver returns documents as bson binary Buffer objects, `default:false`
A collection can be renamed with collection method rename
collection.rename(new_name, callback);
Passing the optional dropTarget boolean as the thrid parameter will allow overwritting of existing collections
collection.rename(new_name, {dropTarget:true}, callback);
Records can be erased from a collection with remove
collection.remove([[query[, options]], callback]);
Where
A collection can be dropped with drop
collection.drop(callback);
or with dropCollection
db.dropCollection(collection_name, callback)