I need to merge customer and product data in the same request from a system using Mongoose with MongoDB in Nest JS.
Here is an example of how the data is structured in the database:
"_id": "621d2137abb45e60cf33a2d4",
"product_id": [
"621cedbf79d68fb4689ef0cb",
"621cedbf79d68fb4689ef0cb",
"621cedbf79d68fb4689ef0cb"
],
"client_id": "621d19f890ec693f1d553eff",
"price": 597,
"__v": 0
Below is my service implementation:
findAll() {
return this.sealsModel.find().exec();
}
I tried the following approach, but it didn't work as expected:
findAll() {
var a = this.sealsModel.find().exec()
return this.sealsModel.aggregate([{
$lookup: {
from: 'seals',
localField: 'client_id',
foreignField: '_id',
as: 'client'
}
}])
.exec()
}
The above method returned the following output:
"_id": "621d3e0a1c3bcac85f79f7cc",
"product_id": [
"621cedbf79d68fb4689ef0cb",
"621cedbf79d68fb4689ef0cb",
"621cedbf79d68fb4689ef0cb"
],
"client_id": "621d19f890ec693f1d553eff",
"price": 597,
"__v": 0,
"client": []