So far we have created an instance, and put some data to it, but it only exists in the application. Saving it to the database is a really simple operation.
After we have created our newUser
and given it some data, we save it in the following way:
newUser.save( function( err ){ if(!err){ console.log('User saved!'); } });
This .save
method is an example of an instance method, because it operates directly on the instance, rather than the model. Note that the parameter it takes is a callback function to run when the save operation has been made to the database. As we saw back in Chapter 1, Introducing Mongoose to the Technology Stack, writing to a database is a blocking operation, but the .save
method provides a non-blocking asynchronous way of doing this, allowing the Node process to carry on and deal with other requests.
This is a good thing for our application, but it means that any operations that you want...