2

I have a document like

 {
 "_id": ObjectId("5b570181d2ccda05180055ed"),
 "VehicleNumber": "7656hhh",
 "DriverDetails": [
 {
 "DriverEntryId": "5016cfe5-e5ca-f9e1-35c6-1ab1d09230fb",
 "DriverId": ObjectId("5b488d70d2ccda01840078e6"),
 "StartDate": ISODate("2018-07-17T22:00:00.0Z"),
 "EndDate": ISODate("2018-07-27T22:00:00.0Z"),
 "Status" : "Active"
},
 {
 "DriverEntryId": "772cb165-33e3-6d92-ff04-007785e52a84",
 "DriverId": ObjectId("5b507122d2ccda04580006ca"),
 "StartDate": ISODate("2018-07-02T22:00:00.0Z"),
 "EndDate": ISODate("2018-07-18T22:00:00.0Z"),
 "Status": "Active"
},
 {
 "DriverEntryId": "12d1fa8a-9987-2c27-119e-80dd434c0534",
 "DriverId": ObjectId("5b488d70d2ccda01840078e6"),
 "StartDate": ISODate("2018-06-24T22:00:00.0Z"),
 "EndDate": ISODate("2018-07-31T22:00:00.0Z"),
 "Status": "Active" 
} 
],
..
}

I want to make status "Inactive" for all embedded document. I have tried below lines of code. It is not working... Please help!!!

 $this->collection->updateOne(
 array('_id' => new MongoDB\BSON\ObjectID($this->id)
 ),
 array('$set' => 
 array('DriverDetails.$.Status' => 'Inactive'
 )
 )
 );
Ashh
46.6k16 gold badges111 silver badges137 bronze badges
asked Jul 26, 2018 at 9:31

1 Answer 1

2

You need to use $[] the all positional operator to update every element in an array

$this->collection->updateOne(
 array('_id' => new MongoDB\BSON\ObjectID($this->id)),
 array('$set' => array('DriverDetails.$[].Status' => 'Inactive'))
)

Equivalent to JS

db.collection.update(
 { "_id": "546djdhu99wijfejf9euf94ef" },
 { "$set": { "DriverDetails.$[].Status": "Inactive" }}
)
answered Jul 26, 2018 at 9:36
Sign up to request clarification or add additional context in comments.

1 Comment

I am using 3.6 version.... it throws error message "<b>Fatal error</b>: Call to undefined method MongoDB\Collection::update()"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.