【发布时间】:2020-02-12 00:33:54
【问题描述】:
我有一个对象数组。
const arr = [
{
name: 'somename',
age: 25,
},
{
name: 'othername',
age: 15,
},
]
当我像这样更新我的收藏时:
MyCollection.insertMany(arr);
它工作正常。该集合有 2 个 Objects 对应于 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}`);
这可行,但 temp 集合仅包含 1 个对象(而不是 2 个),并且 1 个对象有 2 个字段,而每个字段又有 2 个字段。
看起来像这样:
主要收藏:
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
感谢您的帮助,
【问题讨论】:
标签: javascript node.js json mongodb bson