Hi,
I have a document in the below structure in collection A and I am using Database triggers with aggregation to transform the data and push the transformed data to collection B.
Document in Collection A:
{
"_id" : "BRS1736451",
"BookingNum" : "BRS1736451",
"AccountId" : "0010Q00000xMDB6QAO",
"BookingLines" : [
{
"BookingLines" : {
"BookingLine_id" : 11289009,
"ParentLineId" : null,
"MarketKey" : "Brazil"
}
},
{
"BookingLines" : {
"BookingLine_id" : 11289014,
"ParentLineId" : null,
"MarketKey" : "Brazil"
}
}
]
}
In the collection B, I need below changes
- Create own mongo db bson id
- all fields should be in camel case
- Booking Lines in collection A is Array of nested BookingLines Objects. But, in collection B I just need just array of objects as mentioned above.
Transformed Data needed in Collection B will look like below:
{
"_id" : ObjectId("5f9ff6744bcc4ae435f10760"),
"bookingNumber" : "BRS1736451",
"accountId" : "0010Q00000xMDB6QAO",
"bookingLines" : [
{
"bookingLineId" : 11289009,
"lineId" : null,
"marketKey" : "Brazil"
},
{
"bookingLineId" : 11289014,
"lineId" : null,
"marketKey" : "Brazil"
}
]
}
I am trying to use the below code which is working fine for first 2 requirements but for the 3rd requirement i.e. booking Lines are not getting transformed according to my expectation.
Code snippet:
exports = function(fullDocument){
console.log('BookingNumber: ' + fullDocument.BookingNum);
const pipeline = [
{
$project: {
_id: 0,
bookingNumber: fullDocument.BookingNum,
accountId: fullDocument.AccountId,
bookingLines: fullDocument.BookingLines,
}
},
{
$addFields: {
bookingLines: {
$map: {
input: "$bookingLines",
as: "bookingLine",
in: {
bookingLineId: "$$bookingLine.BookingLine_id",
parentBookingLineId: "$$bookingLine.ParentBookingLine_id",
marketKey: "$$bookingLine.MarketKey"
}}}
}},
{
$merge: {
into: 'B',
on: 'bookingNumber',
whenMatched: 'replace',
whenNotMatched: 'insert'
}
}
];
const collectionA = context.services.get("Cluster0").db("XXXX").collection("A");
return collectionA.aggregate(pipeline).toArray().then(() => {
console.log(`Successfully moved ${fullDocument.bookingNumber} data to B collection.`);
})
.catch(err => console.error(`Failed to move ${fullDocument.bookingNumber} data to B collection: ${err}`));
};
Please help me on this.
Thanks,
Vinay