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

192
Vistas
How to get data from multiple collections in mongodb using mongoose?

I have 3 collections in mongoDB with the following structure

Users:

"name":""
"email":""
"phone":""
"city":""
"customerID":""

Ships:

"address":""
"city":""
"pincode":""
"porderID":""
"customerID":""

Orders:

"productName":""
"quantity":""
"pricing":""
"mrp":""
"porderID":""
"customerID":""

How do I write a get request or an aggregate function that returns all customerIDs in a json like this?

[{

customerID:"",
BuyOrder:[{
porderID:"",
productName:"",
quantity:""
ShipmentDetail:[
address:""
city:""
pincode:""
]
}],

customerID:"",
BuyOrder:[{
porderID:"",
productName:"",
quantity:""
ShipmentDetail:[
address:""
city:""
pincode:""
]
}],

}]

Any suggestions would be helpful.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Is not quite clear what do you want... without an input/output example, without a valid JSON... is not easy but I think you are looking for something like this:

This query is a nested $lookup where for each user join the collection with orders using the customerID, and also, orders is joined with ships using customerID too.

db.users.aggregate([
  {
    "$lookup": {
      "from": "orders",
      "localField": "customerID",
      "foreignField": "customerID",
      "as": "BuyOrder",
      "pipeline": [
        {
          "$lookup": {
            "from": "ships",
            "localField": "customerID",
            "foreignField": "customerID",
            "as": "ShipmentDetail"
          }
        }
      ]
    }
  },
  
])

Example here

You also can add a $project stage to output only fields you want, like this example where the result is:

[
  {
    "BuyOrder": [
      {
        "ShipmentDetail": [
          {
            "address": "",
            "city": "",
            "pincode": ""
          }
        ],
        "porderID": "",
        "pricing": "",
        "productName": "",
        "quantity": ""
      }
    ],
    "customerID": ""
  }
]
about 4 years ago · Juan Pablo Isaza Denunciar
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