Thursday 16 March 2017

Mongoose pull object from array inside array

I am currently using Node.js with Mongoose to manipulate an MongoDB database. I would like remove an object from an array itself inside an array.

var newTable = new module.Table({
  "_id": ObjectId("1"),
  "items": [
    {
      "_id": ObjectId("11"),
      "subscribers": [
        {
          "itemId": ObjectId("111")
        },
        {
          "itemId": ObjectId("112")
        }
      ]
    }
  ]
}).save();

I've tried this query but no effect:

module.Table.update(
  { "items._id": ObjectId("11") },
  { $pull: { "items.$.subscribers": { "itemId": ObjectId("112") } } },
  { "safe": true },
  function (err, obj) {
    if (err) console.log(filename + " @ module.Table.update(): " + err);
  }
);

Please help me how to query.



via JonathanB

No comments:

Post a Comment