আসুন নথি-
সহ একটি সংগ্রহ তৈরি করি> db.demo586.insertOne( ... {"details": [ ... { ... "Name":"Chris", ... "Marks":71 ... }, ... { ... "Name":"Chris", ... "Marks":61 ... }, ... { ... "Name":"David", ... "Marks":81 ... } ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9200fefd2d90c177b5bcc7") } > db.demo586.insertOne( ... {"details": [ ... { ... "Name":"Chris", ... "Marks":71 ... }, ... { ... "Name":"Carol", ... "Marks":61 ... }, ... { ... "Name":"David", ... "Marks":81 ... } ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9200fefd2d90c177b5bcc8") }
Find() পদ্ধতি -
এর সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করুন> db.demo586.find();
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে{ "_id" : ObjectId("5e9200fefd2d90c177b5bcc7"), "details" : [ { "Name" : "Chris", "Marks" : 71 }, { "Name" : "Chris", "Marks" : 61 }, { "Name" : "David", "Marks" : 81 } ] } { "_id" : ObjectId("5e9200fefd2d90c177b5bcc8"), "details" : [ { "Name" : "Chris", "Marks" : 71 }, { "Name" : "Carol", "Marks" : 61 }, { "Name" : "David", "Marks" : 81 } ] }
স্বতন্ত্রভাবে নির্বাচন করার জন্য এবং গণনা করার জন্য নিম্নলিখিত ক্যোয়ারী রয়েছে -
> var q= [ ... { "$unwind": "$details" }, ... { ... "$group": { ... "_id": { ... "Name": "$details.Name", ... "Marks": "$details.Marks" ... }, ... "count": { "$sum": 1 } ... } ... }, ... { ... "$group": { ... "_id": "$_id.Name", ... "distinctV": { ... "$addToSet": { ... "value": "$_id.Marks", ... "numberOfValues": "$count" ... } ... } ... } ... }, ... { ... "$project": { ... "_id": 0, ... "Name": "$_id", ... "distinctV": 1 ... } ... } ... ]; > db.demo586.aggregate(q);
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে{ "distinctV" : [ { "value" : 61, "numberOfValues" : 1 } ], "Name" : "Carol" } { "distinctV" : [ { "value" : 71, "numberOfValues" : 2 }, { "value" : 61, "numberOfValues" : 1 } ], "Name" : "Chris" } { "distinctV" : [ { "value" : 81, "numberOfValues" : 2 } ], "Name" : "David" }