MongoDB笔记 删除操作(详细)

Posted Aurora1217

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB笔记 删除操作(详细)相关的知识,希望对你有一定的参考价值。

目录

1.删除全部

2.删除符合条件

3.删除满足条件的单条文档

4.delete


db.collection.remove(
<query>,//删除文档条件
{
    justOne:<bollean>,//false:删除符合条件的所有文档;true:删除符合条件的第一条文档
    writeConcern:<document>,
    collation:<document>
}
)

 

1.删除全部

命令:

​> db.collection.remove({})

实例:

> db.test.insertMany(
... [
... {item:"铅笔",price:2},
... {item:"钢笔",price:60}
... ]
... )
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("608bbb44f85311b76bd558fa"),
                ObjectId("608bbb44f85311b76bd558fb")
        ]
}
> db.test.find().pretty()
{ "_id" : ObjectId("608bbb44f85311b76bd558fa"), "item" : "铅笔", "price" : 2 }
{
        "_id" : ObjectId("608bbb44f85311b76bd558fb"),
        "item" : "钢笔",
        "price" : 60
}
> db.test.remove({})
WriteResult({ "nRemoved" : 2 })
> db.test.find().pretty()
查询无显示

2.删除符合条件

命令:

> db.collection.remove({条件})

实例:

> db.test.insertMany( [ {item:"铅笔",price:2}, {item:"钢笔",price:60},{item:"毛笔",price:30}] )
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("608bbc55f85311b76bd558fc"),
                ObjectId("608bbc55f85311b76bd558fd"),
                ObjectId("608bbc55f85311b76bd558fe")
        ]
}
> db.test.find().pretty()                                                             
{ "_id" : ObjectId("608bbc55f85311b76bd558fc"), "item" : "铅笔", "price" : 2 }
{
        "_id" : ObjectId("608bbc55f85311b76bd558fd"),
        "item" : "钢笔",
        "price" : 60
}
{
        "_id" : ObjectId("608bbc55f85311b76bd558fe"),
        "item" : "毛笔",
        "price" : 30
}
> db.test.remove({price:{$gt:3}})
WriteResult({ "nRemoved" : 2 })
> db.test.find().pretty()
{ "_id" : ObjectId("608bbc55f85311b76bd558fc"), "item" : "铅笔", "price" : 2 }

3.删除满足条件的单条文档

命令:

> db.collection.remove({条件},{justOne:true})

实例:

> db.test.insertMany( [ {item:"铅笔",price:2}, {item:"钢笔",price:60},{item:"毛笔",price:30}] )
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("608bbdc9f85311b76bd558ff"),
                ObjectId("608bbdc9f85311b76bd55900"),
                ObjectId("608bbdc9f85311b76bd55901")
        ]
}
> db.test.find().pretty()                                                                
{ "_id" : ObjectId("608bbdc9f85311b76bd558ff"), "item" : "铅笔", "price" : 2 }
{
        "_id" : ObjectId("608bbdc9f85311b76bd55900"),
        "item" : "钢笔",
        "price" : 60
}
{
        "_id" : ObjectId("608bbdc9f85311b76bd55901"),
        "item" : "毛笔",
        "price" : 30
}
> db.test.remove({price:{$gt:3}},{justOne:true})
WriteResult({ "nRemoved" : 1 })
> db.test.find().pretty()
{ "_id" : ObjectId("608bbdc9f85311b76bd558ff"), "item" : "铅笔", "price" : 2 }
{
        "_id" : ObjectId("608bbdc9f85311b76bd55901"),
        "item" : "毛笔",
        "price" : 30
}

4.delete

(1)deleteMany()

> db.books.find().pretty()
{
        "_id" : ObjectId("608bc487f85311b76bd5590b"),
        "name" : "<a cat story>",
        "price" : 20,
        "color" : "red"
}
{
        "_id" : ObjectId("608bc487f85311b76bd5590c"),
        "name" : "<crying birds story>",
        "price" : 20,
        "color" : "green"
}
{
        "_id" : ObjectId("608bc487f85311b76bd5590d"),
        "name" : "<big dogs story>",
        "price" : 25,
        "color" : "blue"
}
> db.books.deleteMany({price:20})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.books.find().pretty()
{
        "_id" : ObjectId("608bc487f85311b76bd5590d"),
        "name" : "<big dogs story>",
        "price" : 25,
        "color" : "blue"
}

(2)deleteOne()

> db.books.find().pretty()
{
        "_id" : ObjectId("608bcdbff85311b76bd5590f"),
        "name" : "<a cat story>",
        "price" : 20,
        "color" : "red"
}
{
        "_id" : ObjectId("608bcdbff85311b76bd55910"),
        "name" : "<crying birds story>",
        "price" : 20,
        "color" : "green"
}
{
        "_id" : ObjectId("608bcdbff85311b76bd55911"),
        "name" : "<big dogs story>",
        "price" : 25,
        "color" : "blue"
}
> db.books.deleteOne({price:20})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.books.find().pretty()
{
        "_id" : ObjectId("608bcdbff85311b76bd55910"),
        "name" : "<crying birds story>",
        "price" : 20,
        "color" : "green"
}
{
        "_id" : ObjectId("608bcdbff85311b76bd55911"),
        "name" : "<big dogs story>",
        "price" : 25,
        "color" : "blue"
}

下一篇:MongoDB笔记(七) 索引(详细)

以上是关于MongoDB笔记 删除操作(详细)的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB笔记 聚合(详细)

MongoDB学习笔记-使用 MongoDB 进行 CRUD 操作(上)

MongoDB学习笔记-使用 MongoDB 进行 CRUD 操作(下)

Nodejs学习笔记--- 与MongoDB的交互(mongodb/node-mongodb-native)MongoDB入门

MongoDB快速入门学习笔记7 MongoDB的用户管理操作

MongoDB快速入门学习笔记4 MongoDB的文档查询操作