node.js 猫鼬一次删除多个数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28671260/
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-02 18:27:46  来源:igfitidea点击:

Mongoose delete multiple data at once

node.jsmongodbmongoose

提问by exilonX

I have a big list of ids that I want to delete from mongodb from multiple models, the main idea is that I have the same id for a document in multiple schemas and I would like to delete a document from each model. I am doing it like this:

我有一个很大的 id 列表,我想从多个模型中从 mongodb 中删除,主要思想是我对多个模式中的文档具有相同的 id,我想从每个模型中删除一个文档。我这样做:

_.each(wrongList, function(item) {
        UPUSTP.find({id: item.id}).remove(function(err) {
            if (err)
                console.log("Error while deleting " + err.message);
        })

        UPUANAM.find({id: item.id}).remove(function(err) {
            if (err)
                console.log("Error while deleting " + err.message);
        })

        UPUEXE.find({id: item.id}).remove(function(err) {
            if (err)
                console.log("Error while deleting " + err.message);
        })

        UPUEXO.find({id: item.id}).remove(function(err) {
            if (err)
                console.log("Error while deleting " + err.message);
        })

        UPUPROC.find({id: item.id}).remove(function(err) {
            if (err)
                console.log("Error while deleting " + err.message);
        })
    })

The problem is that I have 14000+ ids in the wrongListand the query works but it takes a lot of time to finish... how can I increase the time of the remove? Can I remove in batch or something like that?

问题是我有 14000 多个 idwrongList并且查询有效,但需要很多时间才能完成...如何增加删除时间?我可以批量删除或类似的东西吗?

回答by Tiziano Rossi

I think that you could use $in operator of mongodb.

我认为您可以使用 mongodb 的 $in 运算符。

http://docs.mongodb.org/manual/reference/operator/query/in/

http://docs.mongodb.org/manual/reference/operator/query/in/

Something like:

就像是:

UTUSTP.remove({_id: {$in: wronglist}}, function(){...}); // and so on

回答by Rodrigo Medeiros

Assuming that you're using lo-dash, you can get a collection of itemids with the_.pluckfunction. Let's call it idsArray. Now you could use of the $inoperator, in an async.parallelcall, using removedirectly from your models, like:

假设您正在使用lo-dash,您可以获得item具有该_.pluck函数的id集合。让我们称之为idsArray。现在,您可以$inasync.parallel通话中remove直接使用您的模型中的运算符,例如:

async.parallel({
  function (callback) {
    UPUSTP.remove({ id: { $in: idsArray } }, function (err) {
      if (err) return callback("Error while deleting " + err.message);
      callback(null, "Some useful message here...");
    });
  },
  . // do the same with the other collections
  .
  .
  function (err, result) {
    // check the error and do somethin useful with the results
  }

First, the $inwill reduce the db calls to one per collection. Then the async.parallelwill run the tasks in parallel and last, the removedirectly from the model will remove the findoperation for each collection.

首先,$in将每个集合的 db 调用减少到一个。然后async.parallel将并行运行任务,最后,remove直接从模型中删除find每个集合的操作。