I got an order document structure like that and i want to populate only product and also add the related options and quantity beside the product object
{
_id:ObjectId('61c45d25bdf9c1389879db9f'),
orderItems: [
{
options:[
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
],
quantity:4,
product:ObjectId('61c5e1e7c2ec39450cdf41b7')
},
{
options:[
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
],
quantity:3,
product:ObjectId('61c5e1e7c2ec39450cdf41b7')
}
]}
this is the structure i’d like to achieve
{
_id:ObjectId('61c45d25bdf9c1389879db9f'),
orderItems: [
{
options:[
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
],
quantity:2,
product:{
name:'lorem ipsum dolor set',
image:'1452877_product.png',
type:'immo file',
price:20
}
},
{
options:[
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
{_id:ObjectId('61fd914d4a236b94f816c27d'),question:'lorem ipsum dolor set amet', answer:'lorem'},
],
quantity:3,
product:{
name:'lorem ipsum dolor set',
image:'1452877_product.png',
type:'immo file',
price:20
}
}
]
}
so i write this query to achieve this
const orders = await Order.aggregate([
{
$lookup: {
from:'products',
let:{
productId:'$orderItems.product',
options:"$orderItems.options",
quantity:"$orderItems.quantity",
},
pipeline:[
{
$match: {
$expr: {$in: ["$_id", "$$productId"]}
}
},
{
$project:{
name:1,
price:1,
image:1,
type:1,
options:"$$options",
quantity:"$$quantity"
}
},
],
as:'orderItems.product'
}
},
{
$unwind:'$user'
}
])
instead i got this weird output that options and quantity located inside the producta and not only that, the product object has the options and two quantity of the initial object before populating
"orderItems": {
"product": [
{
"_id": "61cfac01b7e712e06a83d89f",
"name": "Product number 3",
"price": 142,
"type": "immo off files",
"image": "image-1639832015424-.png",
"options": [
[
{
"question": "lorem ipsum dolar set amet 1",
"option": "show",
"_id": "61c45cdfbdf9c1389879db6b"
},
{
"question": "lorem ipsum dolar set amet 2",
"option": "habibi",
"_id": "61c45cdfbdf9c1389879db6c"
},
{
"question": "lorem ipsum dolar set amet 3",
"option": "wongati",
"_id": "61c45cdfbdf9c1389879db6d"
},
{
"question": "lorem ipsum dolar set amet 4",
"option": "gortan",
"_id": "61c45cdfbdf9c1389879db6e"
}
],
[
{
"question": "lorem ipsum dolar set amet 1",
"option": "show",
"_id": "61c45cefbdf9c1389879db93"
},
{
"question": "lorem ipsum dolar set amet 2",
"option": "habibi",
"_id": "61c45cefbdf9c1389879db94"
},
{
"question": "lorem ipsum dolar set amet 3",
"option": "wongati",
"_id": "61c45cefbdf9c1389879db95"
},
{
"question": "lorem ipsum dolar set amet 4",
"option": "gortan",
"_id": "61c45cefbdf9c1389879db96"
}
]
],
"quantity": [
2,
3
]
},
so please you can tell me what’s wrong about my query and why this weird output happened