Aggregation in nodejs

In MongoDb i can get the aggregate results:
Query in MongoDB compass:

db.products.aggregate([
{
$lookup: {
from: “storeorders”,
localField: “_id”,
foreignField: “productid”,
as: “StoreOrderDetails”,
},
},
])


Query Result MongoDB compass:
StoreOrderDetails is populated with data


{ _id: ObjectId(“62d9117456eb3251243b8466”),
skuid: ‘2’,
product: ‘apple2’,
origin: ‘mexico2’,
price: 52,
isActive: true,
datetime: 2019-05-29T21:19:15.187Z,
createdAt: 2022-07-21T08:42:28.207Z,
updatedAt: 2022-07-21T08:42:28.207Z,
__v: 0,
StoreOrderDetails:
[ { _id: ObjectId(“62d968e672d0ab397447c5c3”),
productid: ObjectId(“62d9117456eb3251243b8466”),
skuid: ‘2’,
currQty: 4,
newQty: 6,
appQty: 0,
orderstatus: ‘submittedByStore’,
subBy: ‘arun’,
datetime: 2019-04-29T21:19:15.187Z,
storeName: ‘store1’,
storeAddress: ‘chicago,stree’,
cityName: ‘chicago’,
createdAt: 2022-07-21T14:55:34.112Z,
updatedAt: 2022-07-21T14:55:34.112Z,
__v: 0 } ] }


But in Node/express.js file , StoreOrderDetails is not populated.
Code is below


//aggregate-PRODUCTS
router.route(“/”).get((req, res) => {

Product.aggregate([
{
$lookup: {
from: “Storeorder”,
localField: “_id”,
foreignField: “productid”,
as: “StoreOrderDetails”,
},
},
])
.then((storeorder1) => {
res.json(storeorder1);
})
.catch((err) => res.status(400).json(“Error” + err));
});


get results http://localhost:5000/storeorder:
below the “StoreOrderDetails”: has an empty array


[
{
“_id”: “62d9117456eb3251243b8466”,
“skuid”: “2”,
“product”: “apple2”,
“origin”: “mexico2”,
“price”: 52,
“isActive”: true,
“datetime”: “2019-05-29T21:19:15.187Z”,
“createdAt”: “2022-07-21T08:42:28.207Z”,
“updatedAt”: “2022-07-21T08:42:28.207Z”,
“__v”: 0,
“StoreOrderDetails”:
},
]

Simple

is not the same as

1 Like