विशिष्ट एलिमेंट्स प्राप्त करने के लिए डॉट नोटेशन के साथ $match का उपयोग करें।
नमूना डेटा बनाना (एम्बेडेड ऐरे)
> db.demo641.insert( {
ProductId:101,
"ProductInformation": [
{ ProductName:"Product-1", "ProductPrice":1000 },
{ ProductName:"Product-2", "ProductPrice":500 },
{ ProductName:"Product-3", "ProductPrice":2000 },
{ ProductName:"Product-4", "ProductPrice":3000 }
]
} );
दस्तावेज़ देखना
> db.demo641.find();
{ "ProductId" : 101, "ProductInformation" : [ { "ProductName" : "Product-1", "ProductPrice" : 1000 }, { "ProductName" : "Product-2", "ProductPrice" : 500 }, { "ProductName" : "Product-3", "ProductPrice" : 2000 }, { "ProductName" : "Product-4", "ProductPrice" : 3000 } ] }
कीमत 1000 या 2000 वाले प्रोडक्ट्स प्राप्त करने की क्वेरी
> db.demo641.aggregate([
{$unwind: "$ProductInformation"},
{$match: { "ProductInformation.ProductPrice": {$in :[1000, 2000]}} },
{$project: {_id: 0, ProductInformation: 1} }
]).pretty();
परिणाम:
{ "ProductInformation" : { "ProductName" : "Product-1", "ProductPrice" : 1000 } }
{ "ProductInformation" : { "ProductName" : "Product-3", "ProductPrice" : 2000 } }
व्याख्या: $unwind ऐरे को फ्लैट करता है, $match के साथ $in 1000 या 2000 कीमत वाले एलिमेंट्स फ़िल्टर करता है, $project केवल ProductInformation दिखाता है।