$addFields, $map, $divide, $toString और $reduce का उपयोग करके नया फ़ील्ड बनाएं।
डेमो डेटा
db.demo719.insertOne({
"Number":"7374644",
"details" : {
"otherDetails" : [
{ "ProductId" :"102", "ProductPrice" : NumberInt(500) },
{ "ProductId" :"103", "ProductPrice" : NumberInt(2000) }
]
}
});
क्वेरी
db.demo719.aggregate([
{
$addFields:{
productPriceList: {
$reduce: {
input: {
$map: {
input: "$details.otherDetails.ProductPrice",
in: { $toString: { $divide: ["$$this", 5] } }
}
},
initialValue: "",
in: { $concat: ["$$value", "$$this", " \n "] }
}
}
}
},
{ $project: { _id: 0, Number:1, productPriceList:1 } }
])
परिणाम
{ "Number" : "7374644", "productPriceList" : "100 \n 400 \n " }
प्रत्येक कीमत को 5 से विभाजित करके स्ट्रिंग में बदला गया और नई लाइन के साथ जोड़ा गया।