【发布时间】:2016-10-29 02:51:13
【问题描述】:
我在我的 nodeJs 应用程序中使用 mongoskin 在 mongo db 中插入数据。我需要在数据库中插入文档数组并将插入记录的 ID 发送回客户端。我能够插入数据,但无法在结果对象中找到插入记录的 ID。需要帮助来定位结果中的insertedId。我使用下面的代码批量插入。
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
});
});
我的结果是 BatchWriteResult 对象类型。
【问题讨论】:
标签: mongodb bulkinsert mongoskin