node.js - How to get and update a specific object inside an array with Mongoose? -
i'm having real trouble mongodb , mongoose queries. thing is, have menu
schema:
var menuschema = new schema({ userid: string, name: string, meals: [{ day: number, hour: number, minute: number, aliments: [{ name: string, amount: number, }], }], });
so i'd know how specific meal or aliment , how update them throught _id
value, automatically generated when add them.
thank much.
david.
i working meals, not aliments. i'm doing meals:
menu.update({_id: request.params.menu_id, 'meals._id': request.params.meal_id}, {'meals.$.hour': request.body.hour)}, function(error, affected, raw) { response.send(error); });
and aliments:
menu.update({_id: request.params.menu_id, 'meals.aliments._id': request.params.aliment_id}, {'meals.aliments.$': {name: request.body.name, amount: request.body.amount}}, function(error, affected, raw) { response.send(error); });
edit: ok looked answer on other posts , found out solution.
looks $ positional operator not work multi-level arrays. actually, works 1-level arrays (that's why works meals doesn't aliments).
so guess solution splitting meals or aliments in documents , reference them mealid or alimentid.
i hope add multi-level positional operator soon. great.
Comments
Post a Comment