0

I have this mongo structure in ehich i would like to check 3 values that are _id, product_type_id and condition_id and the array in which all these condition match it's status should change from 0 to 1.

the following is my mongo data

{
        "_id" : ObjectId("5b17b991c440782b5a218cd1"),
        "product" : [
                {
                        "id" : ObjectId("5b87d2ef40c1c45f88222093"),
                        "product_type_id" : ObjectId("5ae8348b7ae0d9538e45ab46"),
                        "condition_id" : [
                                {
                                        "_id" : ObjectId("5ae978187ff1706f3b7dc47c"),
                                        "status" : 1,
                                        "date_added" : "2018-08-30-11-20-15"
                                }
                        ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-08-30-11-20-15",
                        "date_status_change" : "2018-08-30-11-20-15",
                        "status" : 0
                },
                {
                 ...
                },
                {
                 ...
                }
               ]
}

So far i have tried this , but it's not working

[
 "_id"=>$merchant_id,
 "product" =>
  [
   '$elemMatch' =>
    [
     'product_type_id' => $product_type_id,
     'condition_id' =>
      [
       '$elemMatch' => 
        [
          '_id' => $condition_id,
        ]
      ]
    ]
  ],
  '$set' =>
   [
    'product.$.condition_id.$.status' => 1,
   ]
]
confused_geek
  • 249
  • 1
  • 14
  • You can use `db.colname.update( {_id: merchant_id }, { $set: { 'product.$[pro].condition_id.$[cond].status': 1 } }, { arrayFilters: [ { 'pro.product_type_id': product_type_id }, { 'cond._id': condition_id} ] } )` – s7vr Oct 16 '18 at 10:17
  • but can you give a bit of explanation of what you did here . i am fairly new to mongodb – confused_geek Oct 16 '18 at 10:28
  • Sure. here the idea is to locate the array element applying array filters. At each level you apply array filter which will return the index at that level followed by update to traverse through indexes at different levels and set the value for matching array element. – s7vr Oct 16 '18 at 10:30
  • `{ 'cond._id': condition_id} ] }` here we did'nt specify the whole name that is `condition_id` and still somehow it was still able to recognise it – confused_geek Oct 16 '18 at 10:34
  • It's an alias at that level. pro at product level and cond at condition_id level. If you had one more nested level you could just add that there too. – s7vr Oct 16 '18 at 10:35
  • alright . got it .. thank you so much – confused_geek Oct 16 '18 at 10:37

0 Answers0