3

In MongoDB 3.2 they have give update multiple documents with one query like below

try {
   db.inspectors.updateMany(
      { "inspector" : "J. Clouseau", "Sector" : 4 },
      { $set: { "Patrolling" : false } },
      { upsert: true }
   );
}
catch (e) {
   print(e);
}

I've checked php website for MongoDB for this query not having function for this.

3
  • 1
    php.net/manual/en/class.mongoupdatebatch.php Commented Mar 24, 2016 at 14:08
  • @pr0metheus, thanks for your answer.Let me check it and update to you Commented Mar 24, 2016 at 14:12
  • It works for me although the missing documentation. Commented Jul 19, 2016 at 5:38

2 Answers 2

4

Pay attention since there are two versions of mongodb drivers for php. Mongodb 3.2 requires the mongodb.so driver version, which is different from mongo.so (and now deprecated).

Also, as mentionned in the mongodb.so documentation on php.net :

Application developers should consider using this extension in conjunction with the MongoDB PHP library [...]

This official library provides the usual mongodb CRUD functions, on top of the new mongodb.so driver, and the librarie's documentation also says you can make use updateMany() as you would do it in javascript:

$updateResult = $collection->updateMany(
    ["inspector" => "J. Clouseau", "Sector" => 4 ],
    ['$set' => ['Patrolling' => false]]
);

printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
printf("Modified %d document(s)\n", $updateResult->getModifiedCount());
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use MongoDB_DataObject as example below:

$model = new MongoDB_DataObject('inspectors');

$model->Patrolling = false;

$model->whereAdd("inspector = 'J. Clouseau' and Sector = 4");

$model->update();

1 Comment

Please don't just post a link to some tool or library as an answer. At least demonstrate how it solves the problem in the answer itself.

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.