MongoDB-এ নেস্টেড JSON অ্যারের ভেতরের এলিমেন্ট এক্সেস বা কুয়েরি করার জন্য ডট নোটেশন (dot notation) ব্যবহার করা হয়। এটি পথ (path) হিসেবে ফিল্ডের হায়ারার্কি নির্দেশ করে।
উদাহরণ: কালেকশন ও ডেটা তৈরি
> db.demo687.insert({
CountryName: 'US',
info: {
id: 101,
details: [
{
Name: 'Chris',
SubjectName: 'MongoDB',
otherDetails: { "Marks": 58, Age: 23 }
}
]
}
});
WriteResult({ "nInserted" : 1 })
> db.demo687.insert({
CountryName: 'UK',
info: {
id: 102,
details: [
{
Name: 'David',
SubjectName: 'MySQL',
otherDetails: { "Marks": 78, Age: 21 }
}
]
}
});
WriteResult({ "nInserted" : 1 })
সব ডকুমেন্ট দেখানো
> db.demo687.find();
আউটপুট:
{ "_id" : ObjectId("5ea55658a7e81adc6a0b3962"), "CountryName" : "US", "info" : { "id" : 101, "details" : [ { "Name" : "Chris", "SubjectName" : "MongoDB", "otherDetails" : { "Marks" : 58, "Age" : 23 } } ] } }
{ "_id" : ObjectId("5ea55673a7e81adc6a0b3963"), "CountryName" : "UK", "info" : { "id" : 102, "details" : [ { "Name" : "David", "SubjectName" : "MySQL", "otherDetails" : { "Marks" : 78, "Age" : 21 } } ] } }
নেস্টেড ফিল্ড কুয়েরি (Marks = 58)
> db.demo687.find({ "info.details.otherDetails.Marks": 58 });
আউটপুট
{ "_id" : ObjectId("5ea55658a7e81adc6a0b3962"), "CountryName" : "US", "info" : { "id" : 101, "details" : [ { "Name" : "Chris", "SubjectName" : "MongoDB", "otherDetails" : { "Marks" : 58, "Age" : 23 } } ] } }
মূল পয়েন্টসমূহ
- ডট নোটেশন:
"info.details.otherDetails.Marks"— প্রতিটি লেভেল ডট (.) দিয়ে আলাদা করা হয়। - অ্যারে ফিল্ড (
details) এর ক্ষেত্রে ডট নোটেশন অ্যারের যেকোনো এলিমেন্ট ম্যাচ করলে ডকুমেন্ট রিটার্ন করে। - যদি অ্যারেতে একাধিক এলিমেন্ট থাকে এবং নির্দিষ্ট শর্ত পূরণ করতে হয়, তবে
$elemMatchব্যবহার করুন:db.demo687.find({ "info.details": { $elemMatch: { "otherDetails.Marks": 58 } } }) - প্রজেকশনে ডট নোটেশন:
{ "info.details.otherDetails.Marks": 1 }। - আপডেটে পজিশনাল $:
{ $set: { "info.details.$.otherDetails.Marks": 100 } }।