mongodb的删除语句

转自:http://www.mongoing.com/docs/tutorial/remove-documents.html

删除的方法

MongoDB provides the following methods to delete documents of a collection:

db.collection.remove() Delete a single document or all documents that match a specified filter.
db.collection.deleteOne()

Delete at most a single document that match a specified filter even though multiple documents may match the specified filter.

3.2 新版功能.

db.collection.deleteMany()

删除所有匹配指定过滤条件的文档.

3.2 新版功能.

你可以指定条件或过滤器来找到要删除的文档.这些 filters 使用与读操作相同的语法:

  • query filter document 能够用 <field>:<value> 表达式指定相等条件并以此选出所有包含指定 <value> 的 <field> 的文档:

    { <field1>: <value1>, ... }
    
  • query filter document can use the query operators to specify conditions in the following form:

    { <field1>: { <operator1>: <value1> }, ... }
    

删除的行为表现

索引

Delete operations do not drop indexes, even if deleting all documents from a collection.

原子性

MongoDB中所有的写操作在单一文档层级上是原子的.更多关于MongoDB和原子性的信息,请参见 原子性和事务处理.

示例集合

本页提供了在 mongo shell中删除操作的例子.在:program:mongo shell中运行如下命令以向例子中涉及到的 users 集合填入数据:

注解

如果 users 集合中已经包含了相同 _id 值的文档,你需要在插入示例文档前 drop 该集合( db.users.drop() ).

db.users.insertMany(
  [
     {
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites: { artist: "Picasso", food: "pizza" },
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 85, bonus: 10 }
       ]
     },
     {
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites: { artist: "Miro", food: "meringue" },
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 64, bonus: 12 }
       ]
     },
     {
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 }
       ]
     },
     {
       _id: 4,
       name: "xi",
       age: 34,
       type: 2,
       status: "D",
       favorites: { artist: "Chagall", food: "chocolate" },
       finished: [ 5, 11 ],
       badges: [ "red", "black" ],
       points: [
          { points: 53, bonus: 15 },
          { points: 51, bonus: 15 }
       ]
     },
     {
       _id: 5,
       name: "xyz",
       age: 23,
       type: 2,
       status: "D",
       favorites: { artist: "Noguchi", food: "nougat" },
       finished: [ 14, 6 ],
       badges: [ "orange" ],
       points: [
          { points: 71, bonus: 20 }
       ]
     },
     {
       _id: 6,
       name: "abc",
       age: 43,
       type: 1,
       status: "A",
       favorites: { food: "pizza", artist: "Picasso" },
       finished: [ 18, 12 ],
       badges: [ "black", "blue" ],
       points: [
          { points: 78, bonus: 8 },
          { points: 57, bonus: 7 }
       ]
     }
  ]
)

删除所有文档

要想从集合中删除所有文档,传递一个空的 filter document {} 给 db.collection.deleteMany() 或 db.collection.remove() 方法.

db.collection.deleteMany()

如下的例子使用 db.collection.deleteMany() 方法从 users 集合中删除了 所有 文档:

db.users.deleteMany({})

该方法返回了操作状态的文档:

{ "acknowledged" : true, "deletedCount" : 7 }

For more information and examples, see db.collection.deleteMany().

db.collection.remove()

作为另一种过选择,如下的例子使用了 db.collection.remove() 方法从 users 集合中删除 所有 文档:

db.users.remove({})

要想从一个集合里删除所有文档,使用 drop() 方法删除整个集合,包括索引然后重建该集合和索引或许会更高效.

删除满足条件的所有文档

要想删除匹配删除条件的所有文档,传递 filter 参数给 db.collection.deleteMany() 方法或 db.collection.remove() 方法.

db.collection.deleteMany()

如下例子使用 db.collection.deleteMany() 从 users 集合中删除所有 status 字段等于 "A" 的文档:

db.users.deleteMany({ status : "A" })

该方法返回了操作状态的文档:

{ "acknowledged" : true, "deletedCount" : 3 }

db.collection.remove()

作为另一种选择如下例子使用 db.collection.remove() 从 users 集合中删除所有 status 字段等于 "A" 的文档:

db.users.remove( { status : "P" } )

对于大量的删除操作,把你想要保留的文档复制到一个新的集合然后使用 db.collection.drop() 方法删除原集合或许会更高效。

仅删除一个满足条件的文档

要想最多删除一个满足指定过滤条件的文档(即使多个文档可以满足该指定过滤条件),使用 db.collection.deleteOne() 方法或使用 db.collection.remove() 方法并将 <justOne> 参数设置为 true 或 1.

db.collection.deleteOne()

如下例子使用 db.collection.deleteOne() 删除 第一个 status 字段等于 "A" 的文档:

db.users.deleteOne( { status: "D" } )

db.collection.remove()

作为另一种选择,如下例子使用 db.collection.remove() 并将 <justOne> 参数设置为``1`` 来删除 第一个 status 字段等于 "A" 的文档:

db.users.remove( { status: "D" }, 1)

参见

其他方法

其他方法

如下方法也可以从集合中删除文档:

posted @ 2017-09-20 16:29  追梦的独行者  阅读(4273)  评论(0编辑  收藏  举报