Mongo insertMany confusion ...报告来自CLI

时间:2015-12-26 09:59:51

标签: mongodb

我是Mongo的新手,但我认为我所做的事情会很直接......我在JSON文件中有大量的对象/记录,我想使用multiInsert来将所有这些记录加载到集合中。下面的函数用于首先删除集合,然后执行insertMany:

const importRecords = (db, collection, objects) => {
  const dbCollection = db.collection(collection);
  return new Promise((resolve, reject) => {
    dbCollection.drop()
      .then(() => dbCollection.insertMany(objects))
      .then(resolve)
      .catch(reject);
  });
};

这里称为:

const fs = require('fs');
let markers = JSON.parse(fs.readFileSync('./data/Markers.json', {
  encoding: 'utf8',
}));

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log('Connected correctly to server.');

  importRecords(db, 'marker', markers).then(r => {
    console.log(`inserted ${r.insertedCount} markers`, Object.keys(r));
    db.close();
  });
});

该功能确实运行完成,并正确报告已导入279条记录。但是,这是它出错的地方。如果我转到mongo CLI并输入:

 use test
 db.markers.find()

我完全没有回应。而对于其他集合,它返回集合中的所有记录。作为旁注,我让Fortune.js从这个Mongo DB提供API,它识别并公开/markers的端点,但GET操作显示279条“垃圾”记录......好吧它可能不是垃圾但它肯定不是我试图加入的对象数组。请记住,Fortune.js将尝试在官方JSON-API 1.0规范中表示标记。以下是我得到的一个例子:

  

{         “meta”:{           “数”:279         },         “链接”:{           “自我”:“/ markers”         },         “数据”:[           {             “类型”:“标记”,             “id”:“567de88fe58da85d041b40d5”,             “链接”:{               “self”:“/ markers / _bsontype,ObjectID,id,V%7D%C3%A8%C2%8F%C3%A5%C2%8D%C2%A8%5D%04%1B%40%C3%95, toHexString,函数%20()%20%7B%0A%20%20if(ObjectID.cacheHexString%20%26%26%20this .__ ID)%20return%20this .__ ID%3B%0A 0A%%20%20var%20hexString %20%3D%20 '' %3B%0A 0A%%20%20for%20(VAR%20 1%20%3D%200%3B%20I%20%3C%20this.id.length%3B%20I%2B %2B)%20%7B%0A%20%20%20%20hexString%20%2B%3D%20hexTable%5Bthis.id.charCodeAt(ⅰ)%5D%3B%0A%20%20%7D%0A 0A% %20%20if(ObjectID.cacheHexString)%20this .__ ID%20%3D%20hexString%3B%0A%20%20return%20hexString%3B%0A%7D,get_inc,函数%20()%20%7B%0A% 20%20return%20ObjectID.index%20%3D%20(ObjectID.index%20%2B%201)%20%25%200xFFFFFF%3B%0A%7D,getInc,函数%20()%20%7B%0A %20%20return%20this.get_inc()%3B%0A%7D,生成函数20%(时间)%20%7B%0A%20%20if%20( '编号' %20%21%3D%20typeof% 20time)%20%7B%0A%20%20%20%20time%20%3D%20parseInt(Date.now()%2F1000%2C10)%3B%0A%20%20%7D%0A 0A%%20% 20var%20time4Bytes%20%3D%20BinaryParser.encodeInt(时间%2C%2032%2C%20true%2C%20true)%3B%0A%20%20%* 2F%20for%20time系%20ObjectID%第二十条%20字节%20following%第二十条%20time%20will%20be%20zeroed%20个*%2F%0A%20%20var%20machine3Bytes%20%3D%20BinaryParser.encodeInt(MACHINE_ID%2C%2024%2C% 20false)%3B%0A%20%20var%20pid2Bytes%20%3D%20BinaryParser.fromShort((typeof运算%20process%20%3D%3D%3D%20'undefined'%20%3F%20Math.floor(的Math.random ()%20 *%20100000)%20%3A%20process.pid)%20%25%200xFFFF)%3B%0A%20%20var%20index3Bytes%20%3D%20BinaryParser.encodeInt(this.get_inc()%2C %2024%2C%20false%2C%20true)%3B%0A%0A%20%20return%20time4Bytes%20%2B%20machine3Bytes%20%2B%20pid2Bytes%20%2B%20index3Bytes%3B%0A%7D,的toString,功能%20()%20%7B%0A%20%20return%20this.toHexString()%3B%0A%7D,检查,功能%20()%20%7B%0A%20%20return%20this.toHexString( )%3B%0A%7D,的toJSON,函数%20()%20%7B%0A%20%20return%20this.toHexString()%3B%0A%7D,等于,功能%20equals(otherID)%20%7B %0A%20%20var%20id%3B%0A%0A%20%20if(otherID%20%21%3D%20null%20%26%26%20(otherID%20instanceof%20ObjectID%20%7C%7C%20otherID .toHexString))%20%7B%0A%20%20%20%20id%20%3D%20otherI D.id%3B%0A%20%20%7D%20else%20if(typeof运算%20otherID%20%3D%3D%20'string'%20%26%26%20ObjectID.isValid(otherID))%20%7B %0A%20%20%20%20id%20%3D%20ObjectID.createFromHexString(otherID).ID%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20return% 20false%3B%0A%20%20%7D%0A 0A%%20%20return%20this.id%20%3D%3D%3D%20id%3B%0A%7D,getTimestamp,函数%20()%20% 7B%0A%20%20var%20timestamp%20%3D%20new%20Date()%3B%0A%20%20timestamp.setTime(Math.floor(BinaryParser.decodeInt(this.id.substring(0%2C4)%2C %2032%2C%20true%2C%20true))%20 *%201000)%3B%0A%20%20return%20timestamp%3B%0A%7D,generationTime,1451092111"             }           },

0 个答案:

没有答案