Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Javascript 使用mongorestore将多个文档插入临时集合_Javascript_Node.js_Json_Mongodb_Bson - Fatal编程技术网

Javascript 使用mongorestore将多个文档插入临时集合

Javascript 使用mongorestore将多个文档插入临时集合,javascript,node.js,json,mongodb,bson,Javascript,Node.js,Json,Mongodb,Bson,我有一个对象数组 const arr = [ { name: 'somename', age: 25, }, { name: 'othername', age: 15, }, ] 当我像这样更新我的收藏时: MyCollection.insertMany(arr); 它工作正常。集合有2个与arr变量相对应的对象 我要做的就是将这些数据存储到临时集合中。因此: const fileName = '/tmp/some_data.bson'; c

我有一个对象数组

const arr = [
  {
    name: 'somename',
    age: 25,
  },
  {
    name: 'othername',
    age: 15,
  },
]

当我像这样更新我的收藏时:

MyCollection.insertMany(arr);
它工作正常。集合有2个与arr变量相对应的对象

我要做的就是将这些数据存储到临时集合中。因此:

const fileName = '/tmp/some_data.bson';
const data = BSON.serialize(arr); //arr from above
await fs.writeFile(fileName, data);
await child_process.exec(`mongorestore --drop -d my-db -c my_collection_temp ${fileName}`);
这是可行的,但是临时集合只包含1个对象,而不是2个,并且这1个对象有2个字段,每个字段又有2个字段

有点像这样:

00000000: 3600 0000 075f 6964 005e 4354 a93f 9947  6...._id.^CT.?.G
00000010: 050e 9bfc 0802 6e61 6d65 0009 0000 0073  ......name.....s
00000020: 6f6d 656e 616d 6500 0161 6765 0000 0000  omename..age....
00000030: 0000 0039 4000 3700 0000 075f 6964 005e  ...9@.7...._id.^
00000040: 4354 a93f 9947 050e 9bfc 0902 6e61 6d65  CT.?.G......name
00000050: 000a 0000 006f 7468 6572 6e61 6d65 0001  .....othername..
00000060: 6167 6500 0000 0000 0000 2e40 00         age........@.
主要收藏:

Object1 { name: 'somename', age: 25 }
Object2 { name: 'someothername', age: 15 }
Object 1 {

  0: {
    name: 'somename', age: 25
  }
  1: {
    name: 'someothername', age: 15
  }

}

临时集合:

Object1 { name: 'somename', age: 25 }
Object2 { name: 'someothername', age: 15 }
Object 1 {

  0: {
    name: 'somename', age: 25
  }
  1: {
    name: 'someothername', age: 15
  }

}

当我执行mongorestore-drop-d my db-c my_collection_temp${fileName}时,它只是将缓冲区转储到集合中,但我需要一种方法来忽略它,并像在主集合中一样分散对象

换句话说,我想我想通过mongorestore模拟insertMany

当您调用BSON.serializear时,将数组序列化为单个BSON对象,非常感谢您的帮助

使用bsondump将以这种方式生成的文件与使用mongodump生成的文件进行比较,以导出该集合

mongorestore期望的文件格式是一系列序列化的BSON文档

包含您文章中的2个文档的bson文件如下所示:

00000000: 3600 0000 075f 6964 005e 4354 a93f 9947  6...._id.^CT.?.G
00000010: 050e 9bfc 0802 6e61 6d65 0009 0000 0073  ......name.....s
00000020: 6f6d 656e 616d 6500 0161 6765 0000 0000  omename..age....
00000030: 0000 0039 4000 3700 0000 075f 6964 005e  ...9@.7...._id.^
00000040: 4354 a93f 9947 050e 9bfc 0902 6e61 6d65  CT.?.G......name
00000050: 000a 0000 006f 7468 6572 6e61 6d65 0001  .....othername..
00000060: 6167 6500 0000 0000 0000 2e40 00         age........@.
请注意,第一个文档的大小从字节0开始,一直延伸到字节0x35

第二个文档立即从字节0x36开始,大小为,并扩展到文件末尾的字节0x6c

要生成此文件,您需要依次对每个文档调用BSON.serialize,并将字节附加到输出文件。

当您调用BSON.serializear时,您正在将数组序列化为单个BSON对象

使用bsondump将以这种方式生成的文件与使用mongodump生成的文件进行比较,以导出该集合

mongorestore期望的文件格式是一系列序列化的BSON文档

包含您文章中的2个文档的bson文件如下所示:

00000000: 3600 0000 075f 6964 005e 4354 a93f 9947  6...._id.^CT.?.G
00000010: 050e 9bfc 0802 6e61 6d65 0009 0000 0073  ......name.....s
00000020: 6f6d 656e 616d 6500 0161 6765 0000 0000  omename..age....
00000030: 0000 0039 4000 3700 0000 075f 6964 005e  ...9@.7...._id.^
00000040: 4354 a93f 9947 050e 9bfc 0902 6e61 6d65  CT.?.G......name
00000050: 000a 0000 006f 7468 6572 6e61 6d65 0001  .....othername..
00000060: 6167 6500 0000 0000 0000 2e40 00         age........@.
请注意,第一个文档的大小从字节0开始,一直延伸到字节0x35

第二个文档立即从字节0x36开始,大小为,并扩展到文件末尾的字节0x6c

要生成此文件,需要依次对每个文档调用BSON.serialize,并将字节附加到输出文件中