I have a requirement to merge 2 to 3 documents in the same collection and produce a new document containing copies of data from fields in both. Sample data:
// Document one
{
"jobs": [{
"jobId": 1,
"predictedCompletionStatus": "Late"
},
{
"jobId": 2,
"predictedCompletionStatus": "Early"
},
{
"jobId": 3,
"predictedCompletionStatus": "Early"
}
]
}
// Document two
{
"jobs": [{
"jobId": 1,
"predictedCompletionStatus": "Early"
},
{
"jobId": 2,
"predictedCompletionStatus": "OnTime"
}
]
}
// Merged Document
{
"merged": [{
"jobId": 1,
"predictedCompletionStatus1": "Late",
"predictedCompletionStatus2": "Early"
},
{
"jobId": 2,
"predictedCompletionStatus1": "Early",
"predictedCompletionStatus2": "OnTime"
}
]
}
The documents should be joined on the jobId and then alias the columns from each document in order to differentiate them.
I’m used to doing this kind of thing in SQL where a join would get me what I need. Is it possible to do this? I’ve looked at $lookup
but that seems to need the douments in different collections.