【问题标题】:Which of these mongodb queries is more efficient? for loop vs deleteMany这些 mongodb 查询中哪个更高效? for 循环与 deleteMany
【发布时间】:2021-01-17 22:09:14
【问题描述】:

对于包含 30+ 百万个文档的 mongodb 集合,最好:

1:这样查询多次:

for (const _id of array) {

await Schema.deleteOne({ _id: _id })

}

2:使用$in进行一次查询:

await Schema.deleteMany({ _id: { $in: array } })

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query


    【解决方案1】:

    由于 for 循环需要多次连接到数据库,因此使用 deleteMany 是一种更好的方式。

    deleteOne 进入Mongo docs 它说:

    要删除多个文档,请参阅 db.collection.deleteMany()

    此外,对于小型数据库,for 循环的性能可能还不错,但对于大量数据,这不是一个好主意,因为您多次调用数据库。并且还使用await 它会停止执行,因此效率不高。

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多