Adding Documents by Using Mongoose

You can add documents to the MongoDB library by using either the create() method on the Model object or the save() method on a newly created Document object. The create() method accepts an array of JavaScript objects and creates a Document instance for each JavaScript object, which applies validation and a middleware framework to them. Then the Document objects are saved to the database. The syntax of the create() method is shown below:

create(objects, [callback])

The callback function of the create() method receives an error for the first parameter if it occurs and then additional parameters, one for each document, saved as additional parameters. Lines 27–32 of Listing 16.4 illustrate ...

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.