Saving Document Changes

You have already seen how to use the save() method to add a new document to a database. You can also use it to update an existing object. Often the save() method is the most convenient to use when working with MongoDB because you already have an instance of the Document object.

The save() method detects whether an object is new, determines which fields have changed, and then builds a database request that updates those fields in the database. Listing 16.5 shows how to implement a save() request. It retrieves the word book from the database and capitalizes the first letter, changing the word and first fields.

Notice that doc.isNew in line 8 reports that the document is not new. Also, line 14 reports the modified fields ...

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.