1

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,
                                             ]
                                    ]
                             ])

EDIT 1:

    db.collection_name.collection({_id : ObjectId('5b17b991c440782b5a218cd1'),
                       "product.condition_id.$":{ "$exists": false }},
                       { "$unset": { "product.$": "" }});

still not working

2 Answers 2

2
db.collection_name.update(
    {},
    {$pull : {product : {condition_id : {$size : 0} }}},
    { multi: true } // multi : true will updates multiple documents that meet the query criteria
)

if condition_id array is empty pull the document.

Output:

{
    "_id" : ObjectId("5b17b991c440782b5a218cd1"),
    "vendor_view_id" : 741733,
    "product" : [
            {
                    "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
            }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can transform your json to PHP array json_decode($json) so you'll be able to process it before it turn into json after. json_encode($arr) Note that you can check the depth of the array in your case to see if product.condition_id is empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.