Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose save vs insert vs create

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

My current attempt:

var mongoose = require('mongoose'); var Schema = mongoose.Schema;  var notificationsSchema = mongoose.Schema({     "datetime" : {         type: Date,         default: Date.now     },     "ownerId":{         type:String     },     "customerId" : {         type:String     },     "title" : {         type:String     },     "message" : {         type:String     } });  var notifications = module.exports = mongoose.model('notifications', notificationsSchema);  module.exports.saveNotification = function(notificationObj, callback){     //notifications.insert(notificationObj); won't work     //notifications.save(notificationObj); won't work     notifications.create(notificationObj); //work but created duplicated document } 

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.

like image 683
Maria Jane Avatar asked Jul 10 '16 10:07

Maria Jane


People also ask

What is difference between create and save in Mongoose?

create() is a shortcut for saving one or more documents to the database. MyModel. create(docs) does new MyModel(doc). save() for every doc in docs.

What does save () do in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

What is the difference between insert and create in MongoDB?

Model. create does a . save for each document in the array, resulting in N database calls (where N is the number of documents in the array); Collection. insert performs one large database call.

Why we use Mongoose instead of MongoDB?

Using mongoose , a user can define the schema for the documents in a particular collection. It provides a lot of convenience in the creation and management of data in MongoDB. On the downside, learning mongoose can take some time, and has some limitations in handling schemas that are quite complex.


1 Answers

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.

var mongoose = require('mongoose');  var notificationSchema = mongoose.Schema({     "datetime" : {         type: Date,         default: Date.now     },     "ownerId":{         type:String     },     "customerId" : {         type:String     },     "title" : {         type:String     },     "message" : {         type:String     } });  var Notification = mongoose.model('Notification', notificationsSchema);   function saveNotification1(data) {     var notification = new Notification(data);     notification.save(function (err) {         if (err) return handleError(err);         // saved!     }) }  function saveNotification2(data) {     Notification.create(data, function (err, small) {     if (err) return handleError(err);     // saved!     }) } 

Export whatever functions you would want outside.

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

like image 194
Iceman Avatar answered Sep 18 '22 15:09

Iceman