MongoDB removes an element from an array inside an array of objects

I have a document that looks like this:

{
  "_id" : ObjectId("56fea43a571332cc97e06d9c"),
  "sections" : [
    {
      "_id" : ObjectId("56fea43a571332cc97e06d9e"),
      "registered" : [
        "123",
        "e3d65a4e-2552-4995-ac5a-3c5180258d87"
      ]
    }
  ]
}

I would like to remove 'e3d65a4e-2552-4995-ac5a-3c5180258d87'in the array registeredonly a specific section from _idfrom '56fea43a571332cc97e06d9e'.

My current attempt is something like this, but it just returns the original document unchanged.

db.test.findOneAndUpdate(
{
  $and: [
    {'sections._id': ObjectId('56fea43a571332cc97e06d9e')},
    {'sections.registered': 'e3d65a4e-2552-4995-ac5a-3c5180258d87'}
  ]
},
{
  $pull: {
    $and: [
      {'sections._id': ObjectId('56fea43a571332cc97e06d9e')},
      {'sections.registered': 'e3d65a4e-2552-4995-ac5a-3c5180258d87'}
    ]
  }
})

I looked $pull, but I can’t figure out how to make it work with an array of nested objects containing another array. In the examples, $pulleverything seems to deal with only one level of nesting. How to remove the corresponding entry from an array of an registeredelement in an array sectionswith the help _idthat I provide?

+4
1

, position $. , "" .

db.test.findOneAndUpdate(
    { "sections._id" : ObjectId("56fea43a571332cc97e06d9e") }, 
    { "$pull": { "sections.$.registered": "e3d65a4e-2552-4995-ac5a-3c5180258d87" } } 
)
+2

All Articles