Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushing object into array schema in Mongoose

I have this mongoose schema

var mongoose = require('mongoose');  var ContactSchema = module.exports = new mongoose.Schema({   name: {     type: String,     required: true   },   phone: {     type: Number,     required: true,     index: {unique: true}   },   messages: [   {     title: {type: String, required: true},     msg: {type: String, required: true}   }] }, {     collection: 'contacts',     safe: true }); 

and trying to update the model by doing this:

Contact.findById(id, function(err, info) {     if (err) return res.send("contact create error: " + err);      // add the message to the contacts messages     Contact.update({_id: info._id}, {$push: {"messages": {title: title, msg: msg}}}, function(err, numAffected, rawResponse) {       if (err) return res.send("contact addMsg error: " + err);       console.log('The number of updated documents was %d', numAffected);       console.log('The raw response from Mongo was ', rawResponse);      });   }); 

I'm I not declaring the messages to take an array of objects?
ERROR: MongoError: Cannot apply $push/$pushAll modifier to non-array

Any ideas?

like image 768
user1460015 Avatar asked Mar 25 '13 18:03

user1460015


People also ask

How do I push an array object in MongoDB?

In MongoDB, the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.

What does .save do 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.

Is Mongoose an ORM or ODM?

Mongoose is an ODM that provides a straightforward and schema-based solution to model your application data on top of MongoDB's native drivers.

What is __ V 0 in Mongoose?

This is __v field that is only generated when a document(s) is inserted through mongoose. The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero.


1 Answers

mongoose does this for you in one operation.

Contact.findByIdAndUpdate(     info._id,     {$push: {"messages": {title: title, msg: msg}}},     {safe: true, upsert: true},     function(err, model) {         console.log(err);     } ); 

Please keep in mind that using this method, you will not be able to make use of the schema's "pre" functions.

http://mongoosejs.com/docs/middleware.html

As of the latest mogoose findbyidandupdate needs to have a "new : true" optional param added to it. Otherwise you will get the old doc returned to you. Hence the update for Mongoose Version 4.x.x converts to :

Contact.findByIdAndUpdate(         info._id,         {$push: {"messages": {title: title, msg: msg}}},         {safe: true, upsert: true, new : true},         function(err, model) {             console.log(err);         }     ); 
like image 173
fino Avatar answered Oct 01 '22 08:10

fino