Adding Documents to a Collection

Another common task when interacting with MongoDB databases is inserting documents into collections. To insert a document, you need to first create a JavaScript object that represents the document you want to store. You create a JavaScript object because the BSON format that MongoDB uses is based on JavaScript notation.

Once you have a JavaScript version of your new document, you can store it in the MongoDB database by using the insert() method on an instance of the Collection object that is connected to the database. The following is the syntax for the insert() method:

insert(docs, [options], callback)

The docs parameter can be a single document object or an array of document objects. ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.