简体   繁体   English

在Node.JS中使用和尚将多个文档(批量插入)插入到mongoDB集合中时出现问题?

[英]Issue in inserting multiple documents (Bulk Insert) into a mongoDB collection using monk in Node.JS?

I am trying to insert multiple documents into a collection in mongoDB database, but am only getting only one object ID as response (multiple documents are getting created in the DB). 我试图将多个文档插入到mongoDB数据库的集合中,但是仅获得一个对象ID作为响应(在数据库中创建了多个文档)。

Code: 码:

exports.createRelation = function(relationDoc, db, callback) {
var relations = db.get("relations");
relations
        .insert( relationDoc )
    .error( function ( err ) {
        callback(err, null);
    })
    .success( function ( doc ){
        callback(null, doc);
   });
};

in this the relationDoc would be an array 在这种情况下,relationDoc将是一个数组

Input: 输入:

newRelDocs_Array:  [ { 
    path: [ 53d0b191c5ac61d403b0090d ] 
    tob: 1405343247 },
  { 
    path: [ 53d0b191c5ac61d403b0090d ],
    tob: 1405343247 } ]

Response : 回应:

createRelation(): Success
createRelation_db:  { 
  _id: 546a1d6f65c05d1c37660c4c,
  tob: 1405343247

} }

FYI, I am using NodeJS, and connection to MongoDB with monk. 仅供参考,我正在使用NodeJS,并通过和尚连接到MongoDB。

In your question you say realationDoc is an array which I presume looks something like this: 在您的问题中,您说realationDoc是一个数组,我认为它看起来像这样:

realationDoc = [{
  id: 1,
  relation: 2
 },{
  id: 2, relation: 1
 }];

In monk, the insert function is not capable of the MonogoDB 2.4 built-in function Bulk Insert. 在僧侣中,插入功能不具备MonogoDB 2.4内置功能“批量插入”功能。 See http://docs.mongodb.org/manual/reference/method/Bulk.insert/ 参见http://docs.mongodb.org/manual/reference/method/Bulk.insert/

With your code the way it is, you are creating a collection of a single document that has 2 fields, 1 the _id which is auto-generated and the second field that is your array. 按照您的代码原样,您将创建一个包含2个字段的单个文档的集合,其中1个是自动生成的_id,第二个字段是您的数组。

To do what you want to do, you will need to modify your code like this: 要执行您想做的事情,您将需要像这样修改代码:

for(var i = 0; i<relationDoc.length;i++){
  relations.insert(relationDoc[i])
  //..add promise code for success and error stuff
}

Probably not as efficient as the Bulk Insert provided in the native driver, but should get you to where you need to go with monk. 效率可能不如本机驱动程序中提供的“批量插入”,但应该可以带您到需要和尚的地方。

Monk collection wrapper provides access to the underlying collection object via .col field. 僧侣集合包装器通过.col字段提供对基础集合对象的访问。 You can use it to access the methods not implemented by Monk, such as bulk insert and aggregate: 您可以使用它来访问Monk尚未实现的方法,例如批量插入和聚合:

var relations = db.get("relations");
var docs = [ { 
              path: [ "53d0b191c5ac61d403b0090d" ],
              tob: 1405343247
             }, {
              path: [ "53d0b191c5ac61d403b0090d" ],
              tob: 1405343247
             } ];
relations.col.insert(docs, callback);

Note that with .col you will not be able to use the promises functionality provided by monk, however. 请注意,使用.col您将无法使用和尚提供的promises功能。 See here for more. 看到这里更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM