OPTIONS
翻译或纠错本页面

db.collection.remove()

说明

db.collection.remove()

在集合中删除记录

The db.collection.remove() method can have one of two syntaxes. The remove() method can take a query document and an optional justOne boolean:

db.collection.remove(
   <query>,
   <justOne>
)

这个方法还可以传入一个查询条件和一个可选的删除选项参数:

2.6 新版功能.

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)
Parameter Type Description
query document

Specifies deletion criteria using query operators. To delete all documents in a collection, pass an empty document ({}).

在 2.6 版更改: In previous versions, the method invoked with no query parameter deleted all documents in a collection.

justOne boolean Optional. To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria.
writeConcern document

Optional. A document expressing the write concern. Omit to use the default write concern. See 安全写入.

2.6 新版功能.

在 2.6 版更改: The remove() returns an object that contains the status of the operation.

返回:删除结果对象( 写入结果 )中包含操作状态。

行为

安全写入

在 2.6 版更改.

The remove() method uses the delete command, which uses the default write concern. To specify a different write concern, include the write concern in the options parameter.

注意事项

默认情况下, remove() 操作会删除匹配上 query 条件的所有文档记录。指定 justOne 选项后每次操作只删除一个文档记录。如果想要指定顺序后删除一个记录,可以使用 findAndModify() 方法。

When removing multiple documents, the remove operation may interleave with other read and/or write operations to the collection. For unsharded collections, you can override this behavior with the $isolated operator, which “isolates” the remove operation and disallows yielding during the operation. This ensures that no client can see the affected documents until they are all processed or an error stops the remove operation.

See Isolate Remove Operations for an example.

自限制集合

不能在自限制集合( capped collection )中使用 remove() 方法。

分片集合

All remove() operations for a sharded collection that specify the justOne option must include the shard key or the _id field in the query specification. remove() operations specifying justOne 操作分片集合时,如果条件里没有 shard key 字段 也没有 _id 字段,操作会返回异常。

例子

下面是使用 remove() 方法的例子。

删除一个集合中的所有文档记录

如果想要删除一个集合中的所有文档记录,使用 remove 方法并传入一个空文档( {} )作为参数 。下面的操作会删除 bios collection 集合中的所有文档记录:

db.bios.remove( { } )

This operation is not equivalent to the drop() method.

如果想要删除一个集合中的所有文档记录,使用 drop() 方法效率更加高,它会把整个集合和索引全都删掉,然后再重建集合和索引就行了。

删除匹配条件的所有文档记录

想要删除满足指定条件的文档记录,使用 remove() 方法并传入 <query> 参数:

下面的操作会删除所有 qty 大于 20 的文档记录:

db.products.remove( { qty: { $gt: 20 } } )

自定义写确认级别

下面的操作在使用副本集的 products 集合中删除 所有 qty 大于 20 的文档记录并指定写确认级别( write concern )为 "w: majority"wtimeout 为 5000 毫秒,这时操作会在副本集的大多数成员完成操作或者时间大于5秒时返回。

db.products.remove(
    { qty: { $gt: 20 } },
    { writeConcern: { w: "majority", wtimeout: 5000 } }
)

删除满足条件的单个记录

想要删除满足条件的第一个文档记录,使用 remove 方法,传入 query 条件参数并把 justOne 设置成 true1

下面的操作会删除 products 集合中 qty 大于 20 的第一个记录:

db.products.remove( { qty: { $gt: 20 } }, true )

Isolate Remove Operations

To isolate the query, include $isolated: 1 in the <query> parameter as in the following examples:

db.products.remove( { qty: { $gt: 20 }, $isolated: 1 } )

写入结果

在 2.6 版更改.

成功时的返回结果

The remove() returns a WriteResult object that contains the status of the operation. Upon success, the WriteResult object contains information on the number of documents removed:

WriteResult({ "nRemoved" : 4 })

写确认异常

如果 remove() 操作遇到了写确认异常,返回结果中会包含 WriteResult.writeConcernError 字段:

WriteResult({
   "nRemoved" : 21,
   "writeConcernError" : {
      "code" : 64,
      "errInfo" : {
         "wtimeout" : true
      },
      "errmsg" : "waiting for replication timed out"
   }
})

和写入确认无关的异常

如果 remove() 操作遇到了与写确认无关的异常,返回结果中会包含 WriteResult.writeError 字段:

WriteResult({
   "nRemoved" : 0,
   "writeError" : {
      "code" : 2,
      "errmsg" : "unknown top level operator: $invalidFieldName"
   }
})