使用mongoose在MongoDB中批量upsert [英] Bulk upsert in MongoDB using mongoose

查看:1186
本文介绍了使用mongoose在MongoDB中批量upsert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有选项用mongoose执行批量upserts?所以基本上有一个数组并插入每个元素,如果它不存在或更新它,如果它存在? (我正在使用海关_id)

Is there any option to perform bulk upserts with mongoose? So basically having an array and insert each element if it not exists or update it if it exists? (I am using customs _ids)

当我使用 .insert 时,MongoDB会为重复键返回错误E11000(应该更新)。插入多个新文档可以正常工作:

When I do use .insert MongoDB returns an error E11000 for duplicate keys (which should be updated). Inserting multiple new document works fine though:

var Users = self.db.collection('Users');

Users.insert(data, function(err){
            if (err) {
                callback(err);
            }
            else {
                callback(null);
            }
        });

使用 .save 会返回错误,该参数必须是单个文档:

Using .save returns an error that the parameter must be a single document:

Users.save(data, function(err){
   ...
}

这个答案表明没有这样的选择,但是它特定于C#并且已经有3年了。所以我是想知道是否有任何选择使用mongoose吗?

This answer suggest there is no such option, however it is specific for C# and also already 3 years old. So I was wondering if there is any option to do that using mongoose?

谢谢!

推荐答案

特别是在mongoose中,或者至少在编写时没有。至于2.6版本的MongoDB shell实际上使用了批量操作API引擎盖,因为它适用于所有通用助手方法。在它的实现中,它首先尝试这样做,如果是旧版本检测到erver然后对遗留实现进行回退。

Not in "mongoose" specifically, or at least not yet as of writing. The MongoDB shell as of the 2.6 release actually uses the "Bulk operations API" "under the hood" as it were for all of the general helper methods. In it's implementation, it tries to do this first, and if an older version server is detected then there is a "fallback" to the legacy implementation.

所有mongoose方法当前使用遗留实现或写入关注响应和基本的遗留方法。但是从任何给定的mongoose模型中都有一个 .collection 访问器,它本质上从底层节点本机驱动程序访问集合对象,其中实现了mongoose:

All of the mongoose methods "currently" use the "legacy" implementation or the write concern response and the basic legacy methods. But there is a .collection accessor from any given mongoose model that essentially accesses the "collection object" from the underlying "node native driver" on which mongoose is implemented itself:

 var mongoose = require('mongoose'),
     Schema = mongoose.Schema;

 mongoose.connect('mongodb://localhost/test');

 var sampleSchema  = new Schema({},{ "strict": false });

 var Sample = mongoose.model( "Sample", sampleSchema, "sample" );

 mongoose.connection.on("open", function(err,conn) { 

    var bulk = Sample.collection.initializeOrderedBulkOp();
    var counter = 0;

    // representing a long loop
    for ( var x = 0; x < 100000; x++ ) {

        bulk.find(/* some search */).upsert().updateOne(
            /* update conditions */
        });
        counter++;

        if ( counter % 1000 == 0 )
            bulk.execute(function(err,result) {             
                bulk = Sample.collection.initializeOrderedBulkOp();
            });
    }

    if ( counter % 1000 != 0 )
        bulk.execute(function(err,result) {
           // maybe do something with result
        });

 });

mongoose方法实际上意识到连接可能实际上并未实现然后排队直到完成。你正在挖掘的本机驱动程序没有做出这种区分。

The main catch there being that "mongoose methods" are actually aware that a connection may not actually be made yet and "queue" until this is complete. The native driver you are "digging into" does not make this distinction.

所以你必须要知道连接是以某种方式或形式建立的。但是只要你小心你正在做的事情,你就可以使用本机驱动程序方法。

So you really have to be aware that the connection is established in some way or form. But you can use the native driver methods as long as you are careful with what you are doing.

这篇关于使用mongoose在MongoDB中批量upsert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆