更新文档
MongoDB中的文档是以JSON风格的BSON格式存储的。要更新文档,首先需要选择一个集合(collection)并指定要更新的条件。以下是一个更新文档的示例:
<?php
// 选择集合
$collection = $database->selectCollection('mycollection');
// 更新条件
$filter = ['name' => 'John'];
// 更新的值
$update = ['$set' => ['age' => 30]];
// 执行更新操作
$collection->updateOne($filter, $update);
?>
在上面的代码中,我们选择了名为mycollection
的集合,并且指定更新条件name='John'
,同时使用了$set
操作符来设置新的值,即将John的年龄更新为30岁。updateOne
方法用于执行更新操作。