node.js - Mongoose select array item in array -
i'm having issue trying select array element id without having parent id. using following example, want select section _id of 5 save change it. here looks like:
site: { menus: [ { _id: 1 sections:[ { _id: 1 }, { _id: 2 }, { _id: 3 }, { _id: 4 } ] }, { _id: 2 sections:[ { _id: 5 } ] } ] } schemas.site.findone({ _id: req.session.siteid, 'menus.sections._id': req.params.sectionid }).select('menus.$.sections').exec(function(err, site){ if (err) return next(err); var section = site.menus[0].sections.id(req.params.sectionid); // update things section.whatever = something; // save changes site.save(); });
the problem works in first menus array, not in second. if try on second menu section (_id: 5), updates in first menu. i'm sure have close problem selecting. these arrays, not embedded models.
update: seems selection working fine. when console.log out section variable, has proper item in array. problem happens when save called. overwrites wrong item. have no save middleware running.
i have working using menuid , sectionid now. updated this:
var menu = site.menus.id(req.body.menuid), section = menu.sections.id(req.params.sectionid);
i leave question unanswered bit in case there better way without needing menuid.
Comments
Post a Comment