Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
Sorting a query after comparing two collections

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!