mongodb - Mongoose update not persisting -


using mongodb , mongoose, can not $push append element array. after run this, check in mongo shell , new append not there. not sure doing wrong:

usermodel.findone({ firstname: "bob" }, 'pets', function(err, user) {   usermodel.update(user, {     $push: {       pets: {           name: "pear", type: "pig"         }     }   }, function(err, numaffected, raw) {       if (err) console.log(err);       console.log('updated ' + json.stringify(numaffected));   }); }); 

mongoose: users.ensureindex({ username: 1 }) { unique: true, background: true }   mongoose: users.findone({ firstname: 'bob' }) { fields: { pets: 1 } }   mongoose: users.update({ pets: [ { type: 'dog', name: 'apple' }, { type: 'cat', name: 'felix' } ], _id: objectid("56a53d45a428cbde3c4299f8") }) { '$push': { pets: { name: 'pear', type: 'pig' } } } {}  updated {"ok":1,"nmodified":0,"n":0} 

> bob = db.users.find()[0] {     "_id" : objectid("56a53d45a428cbde3c4299f8"),     "firstname" : "bob",     "lastname" : "smith",     "username" : "bob123",     "pets" : [         {             "name" : "apple",             "type" : "dog"         },         {             "name" : "felix",             "type" : "cat"         }     ],     "__v" : 0 } >  

update: after changing type field, still not having success. time trying push { $push: { pets: "sssss" } }.

usermodel.findone({ firstname: "bob" }, 'pets', function(err, user) {   usermodel.update(user,     { $push: { pets: "sssss" }},     function(err, numaffected, raw) {       if (err) console.log(err);       console.log('updated ' + json.stringify(numaffected));   }); }); 

here actual mongodb log:

2016-01-29t03:14:37.030+0000 command  [conn17] command curves.$cmd command: update { update: "users", updates: [ { q: { pets: [ { pettype: "pig", name: "pear" } ], _id: objectid('56aad0a3ef5848c231ec80ed') }, u: { $push: { pets: "sssss" } }, upsert: false, multi: false } ], ordered: true, writeconcern: { w: 1 } } ntoskip:0 keyupdates:0 writeconflicts:0 numyields:0 reslen:55 locks:{ global: { acquirecount: { r: 1, w: 1 } }, database: { acquirecount: { w: 1 } }, collection: { acquirecount: { w: 1 } } } protocol:op_query 0ms 

the real reason type used in pets array not correctly. actually, type 1 keyword of mongoose, used mark schema type.

if change type type1 following schema

var userschema = new mongoose.schema({     // ....     pets: [{name: string, type1: string}], }); 

the saved pets should following _id

"pets" : [     {         "name" : "o3",         "type1" : "t3",         "_id" : objectid("56a5df9adea3071b0de83407")     }, 

under case, can following

usermodel.findone({ firstname: "bob" }, 'pets', function(err, user) {   usermodel.update(user, {     $push: {       pets: {           name: "pear", type1: "pig"         }     }   }, function(err, numaffected, raw) { 

and result

"pets" : [     {         "name" : "o3",         "type1" : "t3",         "_id" : objectid("56a5df9adea3071b0de83407")     },     {         "type1" : "pig",         "name" : "pear",         "_id" : objectid("56a5dff3a648301d0db118df")     } ], 

Comments