0

I have an array in mongodb in which i would like to remove a nested array based upon the id's i have

Below is my array structure

{
        "_id" : ObjectId("5b17b991c440782b5a218cd1"),
       
        "product" : [
                {
                        "id" : ObjectId("5b71310eeff5c42f00f8a4e8"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
                        "condition_id" : [
                                {
                                        "_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
                                        "status" : 1,
                                        "date_added" : "2018-08-10-09-55-54"
                                }
                        ],
                        "shipping_cost" : NumberLong(100),
                        "date_added" : "2018-08-10-09-55-54",
                        "date_status_change" : "2018-08-10-09-55-54",
                        "status" : 1
                },
                {
                        "id" : ObjectId("5b76ae3a40c1c449d6409fa3"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae8348b7ae0d9538e45ab46"),
                        "condition_id" : [
                                {
                                        "_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
                                        "status" : 0,
                                        "date_added" : "2018-08-17-11-15-06"
                                },
                                {
                                        "_id" : ObjectId("5ae978187ff1706f3b7dc47c"),
                                        "status" : 0,
                                        "date_added" : "2018-08-17-11-15-06"
                                }
                        ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-08-17-11-15-06",
                        "date_status_change" : "2018-08-17-11-15-06",
                        "status" : 0
                },
       
        ]

for example i would like to delete the a specific array inside condition_id based it's _id and _id of that array block. For example if i want to delete the first condition_id i would have the values ObjectId('5ae977da7ff1706f3b7dc47a') which is the condition_id and ObjectId('5b71310eeff5c42f00f8a4e8') which is the id for that block.

So far i have i have tried this

db.vendor.update({_id:ObjectId('5b17b991c440782b5a218cd1')},     {$pull: {product: {id: ObjectId('5b71310eeff5c42f00f8a4e8')},condition_id:{_id:ObjectId('5ae977da7ff1706f3b7dc47a')}}});

Edit 1:

I tried this .. it's not even matching the array

db.vendor.update(  { "_id": ObjectId("5b17b991c440782b5a218cd1")},{ "$pull": { "product.$.id": ObjectId('5b71310eeff5c42f00f8a4e8') } } );
confused_geek
  • 249
  • 1
  • 14
  • As in the marked dupe, you need to run your update operation as `db.vendor.update({ "_id": ObjectId('5b17b991c440782b5a218cd1'), "product.id": ObjectId("5b76ae3a40c1c449d6409fa3") }, { "$pull": { "product.$.condition_id": { "_id": ObjectId("5ae977da7ff1706f3b7dc47a") } } } )` – chridam Aug 20 '18 at 09:29
  • tried this .. but it's not working – confused_geek Aug 20 '18 at 09:40

0 Answers0