সাব-ডকুমেন্ট ফিল্ডের distinct মানগুলো পেতে ডট (.) নোটেশন ব্যবহার করতে পারেন। সিনট্যাক্সটি নিম্নরূপ:
db.yourCollectionName.distinct("yourOuterFieldName.yourInnerFieldName");কonception বুঝতে ডকুমেন্টসহ একটি কলেকশন তৈরি করি। ডকুমেন্টসহ কলেকশন তৈরির কোয়েরি নিম্নরূপ:
> db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... { ... "StudentId": 101, ... "StudentPersonalDetails": [ ... ... { ... ... "StudentName": "John", ... ... "StudentAge":24 ... ... }, ... ... { ... ... "StudentName": "Carol", ... ... "StudentAge":21 ... ... } ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c90a9abb74d7cfe6392d7d8") } > db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... ... { ... "StudentId": 102, ... "StudentPersonalDetails": [ ... ... { ... ... "StudentName": "Carol", ... ... "StudentAge":26 ... ... }, ... ... { ... ... "StudentName": "Bob", ... ... "StudentAge":21 ... ... } ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c90a9ceb74d7cfe6392d7d9") } > db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... { ... "StudentId": 103, ... "StudentPersonalDetails": [ ... ... { ... ... "StudentName": "Bob", ... ... "StudentAge":25 ... ... }, ... ... { ... ... "StudentName": "David", ... ... "StudentAge":24 ... ... } ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c90a9e6b74d7cfe6392d7da") }find() মেথড ব্যবহার করে কলেকশন থেকে সব ডকুমেন্ট প্রদর্শন করি। কোয়েরিটি নিম্নরূপ:
> db.getDistinctListOfSubDocumentFieldDemo.find().pretty();
আউটপুট নিম্নরূপ:
{ "_id" : ObjectId("5c90a9abb74d7cfe6392d7d8"), "StudentId" : 101, "StudentPersonalDetails" : [ { "StudentName" : "John", "StudentAge" : 24 }, { "StudentName" : "Carol", "StudentAge" : 21 } ] } { "_id" : ObjectId("5c90a9ceb74d7cfe6392d7d9"), "StudentId" : 102, "StudentPersonalDetails" : [ { "StudentName" : "Carol", "StudentAge" : 26 }, { "StudentName" : "Bob", "StudentAge" : 21 } ] } { "_id" : ObjectId("5c90a9e6b74d7cfe6392d7da"), "StudentId" : 103, "StudentPersonalDetails" : [ { "StudentName" : "Bob", "StudentAge" : 25 }, { "StudentName" : "David", "StudentAge" : 24 } ] }সাব-ডকুমেন্ট ফিল্ডের distinct মানগুলো পাওয়ার কোয়েরি নিম্নরূপ:
> db.getDistinctListOfSubDocumentFieldDemo.distinct("StudentPersonalDetails.StudentName");আউটপুট নিম্নরূপ:
[ "Carol", "John", "Bob", "David" ]