The Collection
object represents a collection in the MongoDB database. You use the Collection
object to access items in the collection, add documents, query documents, and much more.
You can get a Collection
object by using the collection()
method on an instance of the Db
object or by passing a Db
object and collection name to the constructor. If the collection isn’t already created on the MongoDB server, you can create it by using the createCollection()
method on the Db
object. For example, all of the following work:
var collection = db.collection()
var collection = new Collection(db, "myCollection")
db.createCollection("newCollection", function(err, collection){ }
Table 13.7 lists the basic methods you can call from a Collection
object. These methods allow you to add and modify documents in the collection, find documents, and delete the collection.