简体   繁体   English

如何在PHP中使用MongoDB的updateMany查询

[英]How to use updateMany query in PHP with MongoDB

In MongoDB 3.2 they have give update multiple documents with one query like below 在MongoDB 3.2中,他们使用下面的一个查询来更新多个文档

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. 我已经检查了php网站上的MongoDB这个查询没有这个功能。

Pay attention since there are two versions of mongodb drivers for php. 请注意,因为php有两个版本的mongodb驱动程序。 Mongodb 3.2 requires the mongodb.so driver version, which is different from mongo.so (and now deprecated). Mongodb 3.2 需要 mongodb.so驱动程序版本,这与mongo.so不同(现在已弃用)。

Also, as mentionned in the mongodb.so documentation on php.net : 另外,正如在php.net上的mongodb.so文档中提到的:

Application developers should consider using this extension in conjunction with the MongoDB PHP library [...] 应用程序开发人员应考虑将此扩展与MongoDB PHP库结合使用[...]

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: 这个官方提供了通常的mongodb CRUD函数,在新的mongodb.so驱动程序之上, librarie的文档也说你可以像在javascript中那样使用updateMany()

$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());

You can use MongoDB_DataObject as example below: 您可以使用MongoDB_DataObject作为示例:

$model = new MongoDB_DataObject('inspectors');

$model->Patrolling = false;

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

$model->update();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM