push an element to an array in a mongoose document

4,364 views
Skip to first unread message

Saiteja Parsi

unread,
Oct 1, 2014, 11:07:09 AM10/1/14
to mongoo...@googlegroups.com

My schema is as follows:

var schema = new mongoose.Schema({
_id: String,
name:String,
to:[{
   name: String,
   message: [{
      content: String
      timestamp: Date
            }]
   }]
});

I need to push the {content: Content, timeStamp: timestamp} to the message array where to.name == "someName".

I tried in this way but could not succeed.Please help me.

User
  .find({_id: id})
  .where('to.name').equals("someName")
  .to.message.push({content: Content, timeStamp: timestamp})
  .exec(function(err, doc){
            if(err) return console.log(err);
            console.log(doc);
        });

Alone Bala

unread,
Oct 6, 2014, 7:27:13 AM10/6/14
to mongoo...@googlegroups.com

try this one 
  var selection = { _id: id};
  var updateQuery = { $push: { contentcontent,timeStamp: timestamp} };
  var options = { safe: true, upsert: true };
var callback=(--
----

}
User.Update(selection, updateQuery, options, callback)
Reply all
Reply to author
Forward
0 new messages