相关文章推荐
儒雅的绿茶  ·  Excel ...·  1 年前    · 
justOne:,//false:删除符合条件的所有文档;true:删除符合条件的第一条文档 writeConcern:, collation:

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笔记(七) 索引(详细)

1.2 NoSQL特征 1.可弹性扩展 2.BASE特征:基本可用性(Basically Available)、可伸缩性(Scalable)、最终一致性(Eventual Consistency)。 3.大数... 清空集合的所有数据db.collections.remove({}) 删除集合 collectionsdb.collections.drop() 删除数据库 db.dropDatabase() 删除一个数据db.collections.deleteOne() 删除多行数据db.collections.deleteMany() 2.for循环向numbers中插入两万条数据 低性能插入 for(var i=1;i<=2000;I++){ db.numbers.insert({n Admin: 管理权限的,添加 root 用户,用来管理其他所有非root 用户的权限,有一些特定的服务器命令必须在admin 库下执行; Local: 这个数据永远不会被赋值,可以用来存储限于本地单台服务器的任何集合; Config:当mongodb 用于分片设置时,config 数据库在内部使用,用于保存分片信息。 注意:mo 小技巧:手动创建一个后缀为bat的文件,文件名任意,内容为: mongod –dbpath=D:\mongo_data,双击此文件就可以启动MongoDB服务,不用每次都到命令行执行启动命令 在启动服务器的shell下可以键入Ctrl-C来完全的停止mongod的运行 还可以使用命令将mongodb作为服务进行安装: D:\mongodb\bin\mongod.exe –