i have collection representing robots holding inventory of products in positional slots, incremented , decremented.
{ _id: "someid", name: "", inventory: [{ productid: "productid1", count: 30 }, { productid: "productid2", count: 56 }, { // ... 55 slots. }] } i have api interact document on put request. request data contain index of inventory update , number decrement by, eg:
[ { "inventory": 3, "inc": -10 }, // remove 10 robot.inventory[3] { "inventory": 54, "inc": -2 }, // remove 2 robot.inventory[10] ] i have following code.
// robots submit api keep products date machineapiv1.addroute('/products', { authrequired: true, rolerequired: 'machine' }, { put: function () { // omit process data above var user = users.findone(this.request.headers['x-user-id']); robots.update(user.profiles.robot, { $inc: { } // lost. }); } }); i can't quite think of way in single update. how can increment multiple arbitrary indexes in mongo document?
mongodb makes simple - specify position in array of sub-documents want update:
robots.update(user.profiles.robot, { $inc: { 'inventory.3.count': -10, 'inventory.54.count': -2 } });
Comments
Post a Comment