Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 获取Bulk.Insert()的插入ID-Mongoskin_Mongodb_Bulkinsert_Mongoskin - Fatal编程技术网

Mongodb 获取Bulk.Insert()的插入ID-Mongoskin

Mongodb 获取Bulk.Insert()的插入ID-Mongoskin,mongodb,bulkinsert,mongoskin,Mongodb,Bulkinsert,Mongoskin,我在nodeJs应用程序中使用mongoskin在mongo数据库中插入数据。我需要在数据库中插入文档数组,并将插入记录的ID发送回客户端。我可以插入数据,但无法在结果对象中找到插入记录的ID。需要帮助才能在结果中找到插入项。我使用下面的代码批量插入 db.collection('myCollection', function (err, collection) { var bulk = collection.initializeUnorderedBulkOp(); for (v

我在nodeJs应用程序中使用mongoskin在mongo数据库中插入数据。我需要在数据库中插入文档数组,并将插入记录的ID发送回客户端。我可以插入数据,但无法在结果对象中找到插入记录的ID。需要帮助才能在结果中找到插入项。我使用下面的代码批量插入

db.collection('myCollection', function (err, collection) {
    var bulk = collection.initializeUnorderedBulkOp();
    for (var i = 0; i < dataArray.length; i++) {
        bulk.insert(dataArray[i]);
    }

    bulk.execute(function (err, result) {
      //TODO: return the Ids of inserted records to the client
      //Client will use these Ids to perform subsequent calls to the nodejs service
    });
});
db.collection('myCollection',函数(err,collection){
var bulk=collection.initializeUnderedBulkop();
对于(var i=0;i

我的结果是一个
BatchWriteResult
对象类型。

建议使用另一个批量API方法,该方法允许您通过调用插入文档的方法,在对象中获取插入文档的
\u id
值。结果对象的格式与文档中给出的格式相同

当没有与条件匹配的文档时,使用选项的更新操作将执行插入操作。如果更新文档没有指定
\u id
字段,MongoDB会添加
\u id
字段,因此您可以检索插入文档的id 在您的

此外,通常不推荐使用大容量插入操作的方式,因为这基本上是在内存中建立的;您希望在管理队列和内存资源方面有更多的控制,而不是依赖于驱动程序的,以及在16MB以下的完整批处理。实现这一点的方法是使用数据数组的循环和一个计数器,这将有助于将批次一次限制为1000个


下面显示了上述方法

function getInsertedIds(result){
    var ids = result.getUpsertedIds();
    console.log(ids); // an array of upserted ids
    return ids;
}

db.collection('myCollection',function(err,collection) {
    var bulk = collection.initializeUnorderedBulkOp(),
        insertedIds = [],
        counter = 0;

    dataArray.forEach(function (data){
        bulk.find(data).upsert().updateOne(data);
        counter++;

        if (counter % 1000 == 0) {
            bulk.execute(function(err, result) {
               insertedIds = getInsertedIds(result);
               bulk = collection.initializeUnorderedBulkOp(); // reset after execute
            });      
        }
    });

    // Clean up the remaining operations in the queue which were 
    // cut off in the loop - counter not a round divisor of 1000
    if (counter % 1000 != 0 ) {
        bulk.execute(function(err, result) {
            insertedIds = insertedIds.concat(getInsertedIds(result));
            console.log(insertedIds);
        });
    }
});

这意味着upsert将在新创建的文档中返回ID,并插入not?-如果是的话,这是非常聪明的。我喜欢!如果使用最新的Node.js驱动程序,那么只有使用该方法的
Bulk.insert()
方法才有可能实现,但是因为它返回了,我知道的解决方法是通过
upsert()
方式使用该方法。@chridam:感谢您的解释和代码。这只适用于第一次运行。如果要再次插入同一文档,则会更新文档而不是插入。我的要求是无论数据是否存在,每次都应该插入dataArray。我试图按1强制插入<代码>bulk.find({}).upsert().updateOne(数据)-它无法工作。2.
bulk.find({food:'bar}).upsert().updateOne(数据)
-我传递的对象永远不会出现在我的集合中。这是非常缓慢的。数组中有20000条记录挂起。当调用插入记录的api时,我的服务的任务就是简单地插入请求中发送的数组。如果同一数组被传递了两次,服务应该盲目地再次插入该数组。例如:假设客户机使用10条记录的数组调用insert api。我插入它。一段时间后,客户机再次使用相同的10条记录调用api(当然这里没有_id字段)。我应该再插入一次。在2次执行结束时,应该有20条记录。您提供的代码在执行2次后仅产生10条记录。