0
{
        "_id" : 123,
        "a" : [
                {
                        "b" : 1,
                        "bb" : 2
                },
                {
                        "c" : 2,
                        "cc" : 3
                }
        ],
        "ab" : [
                {
                        "d" : 4,
                        "dd" : 5
                },
                {
                        "e" : 5,
                        "ee" : 6
                }
        ]
}

Need to remove mongo specific nested document in array for each document

Output should be like: based on inputs _id:123,ab.d=4

{
        "_id" : 123,
        "a" : [
                {
                        "b" : 1,
                        "bb" : 2
                },
                {
                        "c" : 2,
                        "cc" : 3
                }
        ],
        "ab" : [
                {
                        "e" : 5,
                        "ee" : 6
                }
        ]
}
Ashh
  • 44,693
  • 14
  • 105
  • 132
user3383468
  • 151
  • 1
  • 1
  • 6
  • Use `$elemMatch` projection – Ashh Aug 08 '18 at 07:04
  • 1
    Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – Ashh Aug 08 '18 at 07:04

1 Answers1

0

Your are looking for an update with $pull operator (https://docs.mongodb.com/manual/reference/operator/update/pull/)

In your case:

db.mycollection.update({"_id":123}, {$pull: {"ab":{"d":4}}})
Simonluca Landi
  • 931
  • 8
  • 21