简体   繁体   中英

Mongodb Pushing document into array nested in object value

Okay, well I'm building an app that uses nodejs and it has a pretty complex schema. Here's an example of what's going on there's giant document that looks like this:

var docJSON = {
  people:{
    funny:[{
      name:'Joe',
      height:177
    }]
  }
}

whenever I try to push a new object into funny It never gets pushed! No errors come up or anything. The code I'm using right now to push into funny people is:

var funnyJSON = {
   name:'Paul',
   height:200,
}
mongo.get('people').update({_id:'SOME_STRING'},{
    $push:{'people.funny':funnyJSON}
},function(err){
    if(err) throw err;
});

Is your _id a BSON ObjectId? If so, try the following:

var ObjectID = require('mongodb').ObjectID;

mongo.get('people').update({_id: new ObjectID('SOME_STRING')},{
    $push:{'funny':funnyJSON}
},function(err){
    if(err) throw err;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM