Search code examples
node.jstypescriptexpressmongoosemongoose-populate

Retrieve object information within an array with mongoose


I'm making an API Rest with node, express, typescript and mongoose. I have a method GET that return this result:

{
"success": true,
"status": 200,
"message": "categories listed",
"data": [
    {
        "_id": "612650e55fe1ce0de138e2af",
        "products": [
            {
                "_id": "612650e55fe1ce0de138e2b0",
                "productID": {
                    "reviews": [
                        "611e61ba8cb43f7454787ebb",
                        "611e62008cb43f7454787ebc"
                    ],
                    "_id": "610b18f3e2244a187b36f2d7",
                    "title": "PS4",
                    "description": "La mejor consola del mercado del mundo, mundial",
                    "photo": "https://amazon-clone-jparrot.s3.amazonaws.com/1628123519052",
                    "price": 400,
                    "stockQuantity": 23,
                    "__v": 0,
                    "category": "60fc6454b68717acc239cc6a",
                    "owner": "610b9ed8763da4308223aae0",
                    "averageRating": null,
                    "id": "610b18f3e2244a187b36f2d7"
                },
                "quantity": 1,
                "price": 400
            }
        ],
        "owner": {
            "_id": "611d2d39dfcc705972c1ccb8",
            "name": "Jaume",
            "email": "jaumeparrot2@gmail.com",
            "password": "$2a$10$Rv9Rzrff6578feCdDjyeKuarKCSHYRqKp5n5wTi2IWtcLBOupvPgu",
            "__v": 0,
            "address": "611e9ccdf47c7a7a9cb1d5d9"
        },
        "estimatedDelivery": "Wednesday September 1st",
        "__v": 0
    }
]

}

The problem is that I need to retrieve the object "owner", that is, I need to recover this json:

{
"success": true,
"status": 200,
"message": "categories listed",
"data": [
    {
        "_id": "612650e55fe1ce0de138e2af",
        "products": [
            {
                "_id": "612650e55fe1ce0de138e2b0",
                "productID": {
                    "reviews": [
                        "611e61ba8cb43f7454787ebb",
                        "611e62008cb43f7454787ebc"
                    ],
                    "_id": "610b18f3e2244a187b36f2d7",
                    "title": "PS4",
                    "description": "La mejor consola del mercado del mundo, mundial",
                    "photo": "https://amazon-clone-jparrot.s3.amazonaws.com/1628123519052",
                    "price": 400,
                    "stockQuantity": 23,
                    "__v": 0,
                    "category": "60fc6454b68717acc239cc6a",
                    "owner": {
                      "_id": "611d2d39dfcc705972c1ccb8",
                      "name": "Jaume",
                      "about": "My na is Jaume",
                      "__v": 0
                    },
                    "averageRating": null,
                    "id": "610b18f3e2244a187b36f2d7"
                },
                "quantity": 1,
                "price": 400
            }
        ],
        "owner": {
            "_id": "611d2d39dfcc705972c1ccb8",
            "name": "Jaume",
            "email": "jaumeparrot2@gmail.com",
            "password": "$2a$10$Rv9Rzrff6578feCdDjyeKuarKCSHYRqKp5n5wTi2IWtcLBOupvPgu",
            "__v": 0,
            "address": "611e9ccdf47c7a7a9cb1d5d9"
        },
        "estimatedDelivery": "Wednesday September 1st",
        "__v": 0
    }
]

}

For generate this JSON, I'm using this method: https://github.com/jparrot92/amazon-clone-back/blob/master/src/controllers/order.ts


Solution

  • This is the solution:

        const products = await Order.find({ owner: req.user._id })
      .populate('owner')
      .populate({
        path: 'products.productID',
        populate: {
          path: 'owner',
          model: 'Owner',
        },
      })
      .exec();