函数名称:MongoDB\Driver\WriteResult::getMatchedCount()
函数描述:该函数用于获取MongoDB\Driver\WriteResult对象中匹配的文档数量。
适用版本:MongoDB PHP扩展版本1.0.0及以上。
用法示例:
// 假设已经成功连接到MongoDB服务器,选择了数据库和集合
// 构建查询条件
$filter = ['age' => ['$gt' => 18]];
// 构建更新操作
$update = ['$set' => ['status' => 'active']];
// 创建更新请求
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update($filter, $update, ['multi' => true]);
// 执行更新操作
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('db.collection', $bulk);
// 获取匹配的文档数量
$matchedCount = $result->getMatchedCount();
echo "匹配的文档数量:$matchedCount";
在上述示例中,我们首先创建了一个查询条件$filter
和一个更新操作$update
,然后使用MongoDB\Driver\BulkWrite
类构建了一个更新请求$bulk
。接着,我们使用MongoDB\Driver\Manager
类执行了更新操作,并将结果保存在$result
变量中。
最后,我们使用getMatchedCount()
函数从$result
对象中获取了匹配的文档数量,并将其输出到屏幕上。