'Transform the mongoDB data into multiple collections and restore to new server
Problem:
want to replicate mongodb data across servers but also like to alter a few attributes before to restoring.
Server 1 (DB) -> Transform data of collection -> Server 2 (DB)
expectation:
Server 1
collectionA {
_id: string
name: string,
priceValue: number,
orderId: string
}
on Server 2 should be
collectionA {
_id: string
price: number, // alter property name
orderId: string
}
// create new collection
collectionB {
_id: string // new ID
name: string,
orderId: string
}
Solution
I found a npm plugin (mongocopy) that does the same thing, but it is quite slow when dealing with large amounts of data.
Is there any other better solution to migrate the data? Thanks in advance :)
Solution 1:[1]
There is not much to explain really. After you export/import the database to new server, login with mongo shell and split the collection to 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 generates new ObjectId, _id: 1 preserves original value. Docs for $out parameter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Blex |
