mongodb 中的“deletemany”和“remove”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35691647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:39:56  来源:igfitidea点击:

What's the difference between "deletemany" and "remove" in mongodb?

mongodb

提问by look

What's the difference between the two commands here?
db.collection.deleteMany({condition})
db.collection.remove({condition})

这里的两个命令有什么区别?
db.collection.deleteMany({condition})
db.collection.remove({condition})

采纳答案by Dave Amit

As far as I can say,

据我所知,

db.collection.deleteMany

db.collection.deleteMany

Returns:    
   A document containing:
       > A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled
       > deletedCount containing the number of deleted documents

REF: db.collection.deleteMany

REF: db.collection.deleteMany

Where as

然而

db.collection.remove

db.collection.remove

return WriteResult

返回写入结果

And to remove a single document, there a similar command, db.collection.removeOnewhere as with db.collection.removeyou need to set and option called justOneoption to limit delete to 1 document.

要删除单个文档,有一个类似的命令,db.collection.removeOne其中db.collection.remove您需要设置和选项称为justOne选项以将删除限制为 1 个文档。

Otherwise I guess they are similar.

否则我猜他们是相似的。

node.js drivers

node.js 驱动程序

When talking about node.js drivers, removehas been deprecated (and may be removed in future releases) and deleteOneor deleteMany.

在谈论 时node.js driversremove已被弃用(并且可能会在未来版本中删除)和deleteOnedeleteMany

Hope this makes sense ....

希望这是有道理的......

回答by Revol89

They do the same. The difference is the valuesthat return.

他们做同样的事情。不同之处在于返回

With remove():

remove()

> db.ticker.remove({"name": "Bitcoin"})
WriteResult({ "nRemoved" : 2 })

With deleteMany():

deleteMany()

> db.ticker.deleteMany({"name": "Bitcoin"})
{ "acknowledged" : true, "deletedCount" : 2 }