Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda