mardi 8 mai 2018

access a specific object from an Array of objects inside MongoDB using PHP

I have this dataset , in which i want to access only the product array values where _id is ObjectId("5af160d7c440781424330692"), and status is 1 .

{
        "_id" : ObjectId("5af160d7c440781424330692"),
        "bank_details" : {
                "flag" : 0
        },
        "company_details" : {
                "flag" : 0
        },
        "store_details" : {
                "flag" : 0
        },
        "product" : [
                {
                        "id" : ObjectId("5af171dcc440781e18387c33"),
                        "vendor_user_id" : ObjectId("5af160d7c440781424330693"),
                        "product_type_id" : "5ae834a67ae0d9538e45ab47",
                        "condition_id" : [
                                "5ae977da7ff1706f3b7dc47a"
                        ],
                        "shipping_cost" : 110,
                        "date_added" : "2018-05-08-15-16-04",
                        "date_status_change" : "2018-05-08-15-16-04",
                        "status" : 1
                },
                {
                        "id" : ObjectId("5af171dec440781e18387c34"),
                        "vendor_user_id" : ObjectId("5af160d7c440781424330693"),
                        "product_type_id" : "5ae834b17ae0d9538e45ab48",
                        "condition_id" : [
                                "5ae978587ff1706f3b7dc47e",
                                "5ae978767ff1706f3b7dc47f"
                        ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-05-08-15-16-06",
                        "date_status_change" : "2018-05-08-15-16-06",
                        "status" : 0
                }

So far i have tried this

$condition=array('$and' =>
             array(
                        array('_id' => $vendor_id),
                        array('product' => array('$elemMatch' => array('status' => 1)))
                  )
                );`
`$get_active_product_type=$this->collecton_vendor->find($condition, ['projection' => ['product' => true,'_id'=>false]]);
The above query returns the whole array , but i want only the specific part of it , and only those objects where status is 1

Aucun commentaire:

Enregistrer un commentaire