MongoDB-তে $match এবং ডট নোটেশন ব্যবহার করে এম্বেডেড অ্যারে থেকে নির্দিষ্ট এলিমেন্ট পাওয়া
নির্দিষ্ট এলিমেন্ট পেতে $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
... }
... ]
... }
... );
WriteResult({ "nInserted" : 1 })
সকল ডকুমেন্ট প্রদর্শন
> db.demo641.find();
আউটপুট
{
"_id" : ObjectId("5e9c31d46c954c74be91e6e2"),
"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.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
}
}