I'm trying to compare two collections and return data sorted in the order original order sent, using javascript, node.js
Config:
Collection A:
"community": [
{ "_id": "62203a179aef2e193cdde104", "sort": 3, "rId": "621842a5e6c0f8002768cd11" },
{ "_id": "62203a179aef2e193cdde105", "sort": 1, "rId": "62184291e6c0f8002768cd10" },
{ "_id": "62203a179aef2e193cdde106", "sort": 2, "rId": "6218427ae6c0f8002768cd12" }
]
Collection B:
{ "_id": "6218427ae6c0f8002768cd0f", "captionText": "Test 1" },
{ "_id": "62184291e6c0f8002768cd10", "captionText": "Test 2" },
{ "_id": "621842a5e6c0f8002768cd11", "captionText": "Test 3" }
My Code:
let output = []
let findId = community.map(temp => {return temp.rId})
let findX = await CollectionB.find({'_id': {$in: findId}}, '_id captionText')
output.push(...findX)
console.log(output)
This code returns the output in the order [{"Test 1"}, {"Test 2"}, {"Test 3"}]
However I want to return the order based on the sort value in Collection A.
I want this output [{"sort": 1}, {"sort": 2}, {"sort": 3}], along with the data populated from Collection B.
Any help and explanations would be highly appreciated.