在 MongoDB 3.2 中,他们使用一个查询来更新多个文档,如下所示
try {
db.inspectors.updateMany(
{ "inspector" : "J. Clouseau", "Sector" : 4 },
{ $set: { "Patrolling" : false } },
{ upsert: true }
);
}
catch (e) {
print(e);
}
我已经检查了 MongoDB 的 php 网站,因为这个查询没有这个功能。
最佳答案
请注意,用于 php 的 mongodb 驱动程序有两个版本。 MongoDB 3.2 requires mongodb.so驱动程序版本,与 mongo.so 不同(现在已弃用)。
此外,如 php.net 上的 mongodb.so 文档中所述:
Application developers should consider using this extension in conjunction with the MongoDB PHP library [...]
本官library在新的 mongodb.so 驱动程序和 librarie's documentation 之上提供常用的 mongodb CRUD 功能还说你可以像在 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());
关于php - 如何在 PHP 和 MongoDB 中使用 updateMany 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36201997/