Reputation: 746
I tried to iterate array inside object key value using mongodb.but its not working.how to achieve it.
Database code
Upvotes: 2
Views: 783
Reputation: 745
If you have multiple object in orderdetails array then you can use the following query :
db.product.aggregate([
{
$lookup: {
from: "category",
localField: "category_id",
foreignField: "id",
as: "ordersetails"
}
},
{
$project: {
product_name: 1,
product_image: { $arrayElemAt: ["$image", 0] },
ordersetails: 1
}
},
{
$unwind: "ordersetails"
},
{
$project: {
product_name: 1,
product_image: 1,
ordersetails: "$ordersetails.name"}
}
},
]).toArray();
Upvotes: 0
Reputation: 46441
You can use below aggregation
db.product.aggregate([
{ "$lookup": {
"from": "category",
"localField": "id",
"foreignField": "_id",
"as": "ordersetails"
}},
{ "$project": {
"product_name": 1,
"product_image": { "$arrayElemAt": ["$product_image", 0] },
"ordersetails": { "$arrayElemAt": ["$ordersetails.name", 0] }
}}
]).toArray()
Upvotes: 2