mardi 4 septembre 2018

fetch nested array and update fields simultaneously in mongodb PHP

I have the following value in mongodb's collection . Now i want to fetch the single field inside the array where status is 1 and transferred is 0 . But i also want to set transferred to 1 in the same query so that i don't have to write multiple queries .

Following is my collection

{
        "_id" : ObjectId("5b5c268af93afb77f9d904db"),
        "vendor_id" : ObjectId("5b17b991c440782b5a218cd1"),
        "single" : [
                {
                        "product_id" : ObjectId("5af5619d977f8723188a8f81"),
                        "product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
                        "condition_id" : ObjectId("5ae978587ff1706f3b7dc47e"),
                        "selling_price" : 100,
                        "quantity" : "27",
                        "shipping" : "44",
                        "date_added" : "2018-07-28-08-17-14",
                        "date_live" : 0,
                        "status" : 1,
                        "flag" : 0,
                        "transferred" : 0
                },
                {
                        "product_id" : ObjectId("5af2bd44a003533a8abf4e56"),
                        "product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
                        "condition_id" : ObjectId("5ae978187ff1706f3b7dc47c"),
                        "selling_price" : 200,
                        "quantity" : 403,
                        "shipping" : 702,
                        "date_added" : "2018-08-21-10-39-47",
                        "date_live" : 1,
                        "status" : 1,
                        "flag" : 0,
                        "transferred" : 0
                },
                {
                        "product_id" : ObjectId("5af2bd44a003533a8abf4e56"),
                        "product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
                        "condition_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
                        "selling_price" : 100,
                        "quantity" : 809,
                        "shipping" : 11,
                        "date_added" : "2018-09-04-13-44-10",
                        "date_live" : 0,
                        "status" : 0,
                        "flag" : 0,
                        "transferred" : 0
                }
        ]
}

So far i have written this

$pipeline=[
                                [
                                        '$match'=>
                                                [
                                                          'vendor_id'=>$vendor_id,
                                                          'single.status'=>1,
                                                          'single.transferred'=>0
                                                ]
                                ],
                                [
                                   '$project'=> 
                                                [
                                                          'single'=>1
                                                ]
                                ]
                        ];

But i am not able to set the transferred value to 1




Aucun commentaire:

Enregistrer un commentaire