Skip to content
Advertisement

mongoose save vs insert vs create

What are different ways to insert a document(record) into MongoDB using Mongoose?

My current attempt:

JavaScript

Any idea why insert and save doesn’t work in my case? I tried create, it inserted 2 document instead of 1. That’s strange.

Advertisement

Answer

The .save() is an instance method of the model, while the .create() is called directly from the Model as a method call, being static in nature, and takes the object as a first parameter.

JavaScript

Export whatever functions you would want outside.

More at the Mongoose Docs, or consider reading the reference of the Model prototype in Mongoose.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement