Mongoose – Insert Document to MongoDB

Insert Document to MongoDB – To insert a single document to MongoDB, call save() method on document instance. Callback function(err, document) is an optional argument to save() method. Insertion happens asynchronously and any operations dependent on the inserted document has to happen in callback function for correctness.


Following example demonstrates saving document to collection.

Run the above script

Now check your MongoDB if the document is inserted.

A new collection has been created (as it is not already present), and the document is inserted to the collection.


Conclusion :

In this Node.js Mongoose Tutorial, we have learnt to insert document to MongoDB using Mongoose from Node.js. In our next tutorial, we shall learn to insert multiple documents to MongoDB using Mongoose.