Harsh Patel
Harsh Patel

Reputation: 6830

Find and remove nested level

I want to find and remove the value, where my id is "5cc6d9737760963ea07de411"

my data:

[
    {
        "products" : [ {
                    "id" : "5cc6d9737760963ea07de411",
                    "quantity" : 1,
                    "totalPrice" : 100,
                }, 
                {
                    "id" : "5cc6d9737760963ea07de412",
                    "quantity" : 2,
                    "totalPrice" : 200,
                }
        ],
        "isCompleted" : false,
        "_id" : "5cc7e4096d7c2c1f03570a2f"
    },
    {
        "products" : [{
                    "id" : "5cc6d9737760963ea07de414",
                    "quantity" : 1,
                    "totalPrice" : 100,
                }, 
                {
                    "id" : "5cc6d9737760963ea07de4133",
                    "quantity" : 2,
                    "totalPrice" : 200,
                }
        ],
        "isCompleted" : false,
        "_id" : "5cc7e4096d7c2c1f03570a2f"
    }

]

i tried:

Schema.findOneAndUpdate({"products.id": "5cc6d9737760963ea07de411"},
{ $pull:  {"products.$.id": "5cc6d9737760963ea07de411"  })

but it's not working.

Upvotes: 1

Views: 65

Answers (1)

Ashh
Ashh

Reputation: 46451

Now you don't need to use .dot notation. Instead you need to use "absolute" object structures:

Schema.findOneAndUpdate(
  { "products.id": "5cc6d9737760963ea07de411" },
  { "$pull": { "products": { "id": "5cc6d9737760963ea07de411" }}}
)

Upvotes: 2

Related Questions