নেস্টেড অবজেক্ট প্রোপার্টি দিয়ে ফিল্টার: ডট নোটেশন
MongoDB-এ নেস্টেড অবজেক্টের অ্যারে থেকে নির্দিষ্ট প্রোপার্টি মান অনুযায়ী ডকুমেন্ট খুঁজে বের করতে find() এ ডট নোটেশন ব্যবহার করা হয়।
নমুনা ডেটা তৈরি
> db.demo455.insertOne({ "Information": { "Student": [{ "Name": "Chris", "Age": 22 }] }})
> db.demo455.insertOne({ "Information": { "Student": [{ "Name": "David", "Age": 21 }] }})
> db.demo455.insertOne({ "Information": { "Student": [{ "Name": "Bob", "Age": 24 }] }})
> db.demo455.insertOne({ "Information": { "Student": [{ "Name": "Robert", "Age": 21 }] }})
সমস্ত ডকুমেন্ট দেখানো
> db.demo455.find()
আউটপুট:
{ "_id": ObjectId("..."), "Information": { "Student": [{ "Name": "Chris", "Age": 22 }] } }
{ "_id": ObjectId("..."), "Information": { "Student": [{ "Name": "David", "Age": 21 }] } }
{ "_id": ObjectId("..."), "Information": { "Student": [{ "Name": "Bob", "Age": 24 }] } }
{ "_id": ObjectId("..."), "Information": { "Student": [{ "Name": "Robert", "Age": 21 }] } }
নির্দিষ্ট প্রোপার্টি (Age=21) দিয়ে ফিল্টার
> db.demo455.find({ "Information.Student.Age": 21 })
ফলাফল:
{ "_id": ObjectId("..."), "Information": { "Student": [{ "Name": "David", "Age": 21 }] } }
{ "_id": ObjectId("..."), "Information": { "Student": [{ "Name": "Robert", "Age": 21 }] } }
ব্যাখ্যা
Information.Student.Age: ডট নোটেশন দিয়ে নেস্টেড পথ নির্দিষ্ট করা হয়েছে- MongoDB অটোমেটিক অ্যারে (
Student) এর মধ্যে যেকোনো এলিমেন্টে শর্ত মেললে ডকুমেন্ট রিটার্ন করে - যদি অ্যারেতে একাধিক এলিমেন্ট থাকে এবং শর্ত পূরণ করে, তবে ডকুমেন্ট একবারই আসবে
- ইনডেক্স তৈরি:
db.demo455.createIndex({ "Information.Student.Age": 1 })
সিম্পল কোয়েরির জন্য ডট নোটেশন সবচেয়ে সহজ এবং পঠনীয়।