本文介绍了猫鼬(mongodb)批量插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mongoose v3.6+ 现在支持批量插入吗?我已经搜索了几分钟,但与此查询匹配的任何内容都已经使用了几年,答案是明确的否.

Does Mongoose v3.6+ support batch inserts now? I've searched for a few minutes but anything matching this query is a couple of years old and the answer was an unequivocal no.

为了将来参考,答案是使用 Model.create().create() 接受一个数组作为它的第一个参数,因此您可以传递要作为数组插入的文档.

For future reference, the answer is to use Model.create(). create() accepts an array as its first argument, so you can pass your documents to be inserted as an array.

请参阅Model.create() 文档

推荐答案

Model.create() vs Model.collection.insert():更快的方法

Model.create() 如果您要处理非常大的批量,则进行插入是一种糟糕的方式.它会很慢.在这种情况下,您应该使用 Model.collection.insert,它的性能要好得多.根据批量的大小,Model.create() 甚至会崩溃!尝试了一百万个文件,没有运气.使用 Model.collection.insert 只需几秒钟.

Model.create() vs Model.collection.insert(): a faster approach

Model.create() is a bad way to do inserts if you are dealing with a very large bulk. It will be very slow. In that case you should use Model.collection.insert, which performs much better. Depending on the size of the bulk, Model.create() will even crash! Tried with a million documents, no luck. Using Model.collection.insert it took just a few seconds.

Model.collection.insert(docs, options, callback)

  • docs 是要插入的文档数组;
  • options 是一个可选的配置对象 - 参见 文档
  • callback(err, docs) 将在所有文档保存或发生错误后调用.如果成功,docs 是持久化文档的数组.
    • docs is the array of documents to be inserted;
    • options is an optional configuration object - see the docs
    • callback(err, docs) will be called after all documents get saved or an error occurs. On success, docs is the array of persisted documents.
    • 正如 Mongoose 的作者在此处指出的那样,此方法将绕过任何验证程序并直接访问 Mongo 驱动程序.这是您必须进行的权衡,因为您要处理大量数据,否则您根本无法将其插入数据库(请记住,我们在这里讨论的是数十万个文档).

      As Mongoose's author points out here, this method will bypass any validation procedures and access the Mongo driver directly. It's a trade-off you have to make since you're handling a large amount of data, otherwise you wouldn't be able to insert it to your database at all (remember we're talking hundreds of thousands of documents here).

      var Potato = mongoose.model('Potato', PotatoSchema);
      
      var potatoBag = [/* a humongous amount of potato objects */];
      
      Potato.collection.insert(potatoBag, onInsert);
      
      function onInsert(err, docs) {
          if (err) {
              // TODO: handle error
          } else {
              console.info('%d potatoes were successfully stored.', docs.length);
          }
      }
      

      更新 2019-06-22:虽然 insert() 仍然可以正常使用,但它已被弃用,取而代之的是 insertMany().参数完全相同,因此您可以将其用作直接替换,一切都应该正常工作(好吧,返回值有点不同,但您可能无论如何都不会使用它).

      Update 2019-06-22: although insert() can still be used just fine, it's been deprecated in favor of insertMany(). The parameters are exactly the same, so you can just use it as a drop-in replacement and everything should work just fine (well, the return value is a bit different, but you're probably not using it anyway).

      这篇关于猫鼬(mongodb)批量插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:08