MongoDB删除文档

文章目录
[隐藏]

1.MongoDB删除

1.1.删除单条数据

db.collection.deleteOne({}); //删除符合条件的第一条数据
> db.user.find();
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb38"), "name" : "test", "address" : "china" }
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb39"), "name" : "test2", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3a"), "name" : "test3", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3b"), "name" : "test4", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3c"), "name" : "test5", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3d"), "name" : "test5", "address" : "china" }
> db.user.deleteOne({'name':'test5'});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.user.find();
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb38"), "name" : "test", "address" : "china" }
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb39"), "name" : "test2", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3a"), "name" : "test3", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3b"), "name" : "test4", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3d"), "name" : "test5", "address" : "china" }
> 

1.2.删除多条数据

db.user.deleteMany({}) //删除符合条件的所有数据
> db.user.find();
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb38"), "name" : "test", "address" : "china" }
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb39"), "name" : "test2", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3a"), "name" : "test3", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3b"), "name" : "test4", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3d"), "name" : "test5", "address" : "china" }
{ "_id" : ObjectId("5cd0acd3e8271827134eeb3e"), "name" : "test5", "address" : "china" }
{ "_id" : ObjectId("5cd0acd3e8271827134eeb3f"), "name" : "test5", "address" : "china" }
> db.user.deleteMany({"name":"test5"})
{ "acknowledged" : true, "deletedCount" : 3 }
> db.user.find();
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb38"), "name" : "test", "address" : "china" }
{ "_id" : ObjectId("5cd0aa2ee8271827134eeb39"), "name" : "test2", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3a"), "name" : "test3", "address" : "china" }
{ "_id" : ObjectId("5cd0ab69e8271827134eeb3b"), "name" : "test4", "address" : "china" }

2.PHP MongoDB删除数据

使用 remove() 方法来删除文档。
删除col集合中title为test的第一条数据。

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->test;            // 选择一个数据库
$collection = $db->col; // 选择集合
   
// 移除文档
$collection->remove(array("title"=>"test"), array("justOne" => true));

// 显示可用文档数据
$cursor = $collection->find();
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}
?>

3.PHP7 MongoDB删除数据

删除test数据库col集合,name = "test"的第一条数据, name = "test2"的所有数据
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['name' => "test"], ['limit' => 1]);   // limit 为 1 时,删除第一条匹配数据
$bulk->delete(['name' => "test2"], ['limit' => 0]);   // limit 为 0 时,删除所有匹配数据

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.col', $bulk, $writeConcern);
?>
209 人浏览过

发表评论

邮箱地址不会被公开。 必填项已用*标注