node.js - Doing update on MongooseJS Schema Array that is using Populate -
i using mongoose population join documents
var usersschema = schema({ username: string, password: {type: string, select: false }, fname: string, lname: string, role: string, apps: [ { //using populate here app: {type:objectid, ref: "applications"}, pinned: boolean } ] }, { collection : 'user' });
this being called this
findbyid: function(id, items, callback){ user.findbyid(id, items , function(err, doc){ callback(doc); }).populate("apps.app"); }
this works groovy. problem when want push in new app value being populated. doing.
client:
$.ajax({ type: "put", url: userurl + userid, contenttype: "application/json", data: json.stringify({ "app": currentapp.attributes._id, "pinned": false }), success: function(){ console.log("success"); } })
node server:
user.findbyidandupdate(id, { $push : { apps : { pinned:updateobj.pinned, app:updateobj.app } } }, options, function(err, data){ callback(data); });
this seems update correct. issue way being added mongo document
{ "_id": { "$oid": "520953dde4b00c03eeb39950" }, "apps": [ { "app": "5217685be4b061d31fe3cc41", "pinned": true }, { "app": "5208edb2e4b0b41ab826aca1", "pinned": false }, //this added { "pinned": false, "app": { "$oid": "5208ed90e4b0b41ab826ac9f" }, "_id": { "$oid": "5217ac368d334b020000000a" } } ], "fname": "frank", "lname": "miggz", "password": "password123", "role": "2", "username": "blah" }
how i:
remove _id have value of app show looks
"app": "5208ed90e4b0b41ab826ac9f"
just realized values pre-populated should have been in format getting since set "type:objectid" in schema. believe have repopulate match objectid structure
Comments
Post a Comment