mongodb db.collection.remove用法

对于db.collection.remove需要加入加入一个query才能正常运行,否则仅仅运行remove()将会报错,例如:

> db.dbdao_stuff.insert({"_id":1,"a":1,"b":1});
WriteResult({ "nInserted" : 1 })

> db.dbdao_stuff.insert({"_id":2,"a":2,"b":3});
WriteResult({ "nInserted" : 1 })
> db.dbdao_stuff.insert({"_id":3,"a":3,"b":6});
WriteResult({ "nInserted" : 1 })
> db.dbdao_stuff.insert({"_id":4,"a":4,"b":10});
WriteResult({ "nInserted" : 1 })
> db.dbdao_stuff.insert({"_id":5,"a":5,"b":15});
WriteResult({ "nInserted" : 1 })

> db.dbdao_stuff.find();
{ "_id" : 1, "a" : 1, "b" : 1 }
{ "_id" : 2, "a" : 2, "b" : 3 }
{ "_id" : 3, "a" : 3, "b" : 6 }
{ "_id" : 4, "a" : 4, "b" : 10 }
{ "_id" : 5, "a" : 5, "b" : 15 }


db.dbdao_stuff.remove();
2015-05-05T21:20:21.275+0800 E QUERY Error: remove needs a query
at Error ()
at DBCollection._parseRemove (src/mongo/shell/collection.js:305:32)
at DBCollection.remove (src/mongo/shell/collection.js:328:23)
at (shell):1:16 at src/mongo/shell/collection.js:305

如上语句 仅仅运行 db.dbdao_stuff.remove(); 会报错 Error: remove needs a query
> db.dbdao_stuff.remove({});
WriteResult({ "nRemoved" : 5 })
这时候加入 remove({}); 即可删除所有document

db.collection.remove有以下2种用法:

db.collection.remove(
<query>,
<justOne>
)
或者 
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)

db.collection.remove( { type : "food" } )
justone=>TRUE 时仅仅删除一条符合条件的document , writeConcern是关于safe write的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值