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`
collectionName
is the name of the collection (not including the database name as a prefix)db
is the pointer to the corresponding databse object
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 mongodb = require("mongodb"),
mongoserver = new mongodb.Server("localhost"),
db_connector = new mongodb.Db("blog", mongoserver);
db_connector.open(function(err, db){
db.collectionNames(function(err, collections){
console.log(collections); // ["blog.posts", "blog.system.indexes"]
});
});
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);
Records can be erased from a collection with remove
collection.remove([[query[, options]], callback]);
Where
query
is the query that records to be removed need to match. If not set all records will be removedoptions
indicate advanced options. For example use{safe: true}
when using callbackscallback
callback function that gets two parameters - an error object (if an error occured) and the count of removed records
A collection can be dropped with drop
collection.drop(callback);
or with dropCollection
db.dropCollection(collection_name, callback)