问题:
希望在服务器之间复制mongodb数据,但也希望在恢复之前更改一些属性。
服务器1(DB)->转换集合的数据->服务器2(DB)
期望值:
服务器1
collectionA {
_id: string
name: string,
priceValue: number,
orderId: string
}
服务器2上应该是
collectionA {
_id: string
price: number, // alter property name
orderId: string
}
// create new collection
collectionB {
_id: string // new ID
name: string,
orderId: string
}
解决方案
我发现了一个npm插件(mongocopy),它也做同样的事情,但在处理大量数据时速度相当慢。
有没有其他更好的解决方案来迁移数据?提前感谢:)
实际上没有什么可解释的。将数据库导出/导入到新服务器后,使用mongoshell登录并将集合拆分为2:
db.collectionA.aggregate([
{
$project: {
_id: 0,
name: "$name",
orderId: "$orderId"
}
},
{
"$out": "collectionB"
}
]);
db.collectionA.aggregate([
{
$project: {
_id: 1,
price: "$priceValue",
orderId: "$orderId"
}
},
{
"$out": "collectionA"
}
]);
_id: 0
生成新的ObjectId,_id: 1
保留原始值。$out参数的单据