MongoDB remove() function
Translation results:
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() functionsyntax
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() functionexample
以下文档我们执行两次插入操作: >db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'php中文网', url: '//m.sbmmt.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) 使用 find() 函数查询数据: > db.col.find() { "_id" : ObjectId("56066169ade2f21f36b03137"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php中文网", "url" : "//m.sbmmt.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5606616dade2f21f36b03138"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php中文网", "url" : "//m.sbmmt.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } 接下来我们移除 title 为 'MongoDB 教程' 的文档: >db.col.remove({'title':'MongoDB 教程'}) WriteResult({ "nRemoved" : 2 }) # 删除了两条数据 >db.col.find() …… # 没有数据 如果你只想删除第一条找到的记录可以设置 justOne 为 1,如下所示: >db.COLLECTION_NAME.remove(DELETION_CRITERIA,1) 如果你想删除所有数据,可以使用以下方式(类似常规 SQL 的 truncate 命令): >db.col.remove({}) >db.col.find() >