remove

UK[r??mu:v] US[r??muv]

vt.Remove; expel; take off, take off; migrate

vi.Migrate, move; leave

n.Distance, gap; move

MongoDB remove() function syntax

Function: remove() function is used to remove data from the collection

Syntax: db.collection.remove(<query>,< ;justOne>); MongoDB is version 2.6 or later db.collection.remove(<query>, {justOne: <boolean>,writeConcern: <document>})

Parameters: query: (optional) Conditions for deleted documents. justOne : (optional) If set to true or 1, only one document will be deleted. writeConcern: (optional) The level at which the exception is thrown.


MongoDB remove() function example

以下文檔我們執(zhí)行兩次插入操作:

>db.col.insert({title: 'MongoDB 教程', 
    description: 'MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫(kù)',
    by: 'php中文網(wǎng)',
    url: 'http://miracleart.cn',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})
使用 find() 函數(shù)查詢數(shù)據(jù):

> db.col.find()
{ "_id" : ObjectId("56066169ade2f21f36b03137"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫(kù)", "by" : "php中文網(wǎng)", "url" : "http://miracleart.cn", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5606616dade2f21f36b03138"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫(kù)", "by" : "php中文網(wǎng)", "url" : "http://miracleart.cn", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
接下來我們移除 title 為 'MongoDB 教程' 的文檔:

>db.col.remove({'title':'MongoDB 教程'})
WriteResult({ "nRemoved" : 2 })           # 刪除了兩條數(shù)據(jù)
>db.col.find()
……                                        # 沒有數(shù)據(jù)
如果你只想刪除第一條找到的記錄可以設(shè)置 justOne 為 1,如下所示:

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
如果你想刪除所有數(shù)據(jù),可以使用以下方式(類似常規(guī) SQL  的 truncate 命令):

>db.col.remove({})
>db.col.find()
>