mercredi 29 août 2018

Remove mongo array if nested array is empty

i have got a mongo array , in which i want to remove the whole block , if the nested array of that block is empty

I have attached the array below

{
        "_id" : ObjectId("5b17b991c440782b5a218cd1"),
        "vendor_view_id" : 741733,
        "product" : [
                {
                        "id" : ObjectId("5b86546540c1c414543e4333"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae8348b7ae0d9538e45ab46"),
                        "condition_id" : [ ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-08-29-08-08-05",
                        "date_status_change" : "2018-08-29-08-08-05",
                        "status" : 0
                },
                {
                        "id" : ObjectId("5b8654ba40c1c4145d1f5473"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae834b17ae0d9538e45ab48"),
                        "condition_id" : [ ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-08-29-08-09-30",
                        "date_status_change" : "2018-08-29-08-09-30",
                        "status" : 0
                },
                {
                        "id" : ObjectId("5b8655a840c1c415080b0a33"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae834a67ae0d9538e45ab47"),
                        "condition_id" : [
                                {
                                        "_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
                                        "status" : 0,
                                        "date_added" : "2018-08-29-08-13-28"
                                }
                        ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-08-29-08-13-28",
                        "date_status_change" : "2018-08-29-08-13-28",
                        "status" : 0
                }
        ]
}

I would like to delete the array block where product.condition_id is empty

So far i have tried this

$this->collection_name->collection->updateOne([
                                                 '_id' => $vendor_id,
                                                 ],
                                                        [
                                                        '$unset' =>
                                                                        [
                                                                                'product.$.condition_id' =>
                                                                                         [
                                                                                                '$size'=>0,
                                                                                         ]
                                                                        ]
                                                         ])



Aucun commentaire:

Enregistrer un commentaire