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

时间:2016-03-24 14:00:33

标签: php mongodb database

在MongoDB 3.2中,他们使用下面的一个查询来提供多个文档的更新

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

我已经检查过MongoDB的php网站,因为这个查询没有此功能。

2 个答案:

答案 0 :(得分:3)

请注意,因为php有两个版本的mongodb驱动程序。 Mongodb 3.2 requires mongodb.so驱动程序版本,与mongo.so不同(现已弃用)。

另外,正如在php.net上的mongodb.so文档中提到的那样:

  

应用程序开发人员应考虑将此扩展程序与MongoDB PHP library [...]

结合使用

这位官方library在新的mongodb.so驱动程序之上提供了常用的mongodb CRUD函数,librarie's documentation也表示你可以像使用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()); 在javascript:

{{1}}

答案 1 :(得分:-1)

您可以使用MongoDB_DataObject作为示例:

$model = new MongoDB_DataObject('inspectors');

$model->Patrolling = false;

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

$model->update();