একাধিক ফলাফল একত্রিত করতে, MongoDB-তে $group ব্যবহার করুন। আসুন নথি-
সহ একটি সংগ্রহ তৈরি করি> db.demo765.insertOne( ... ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MongoDB", ... "Marks":75 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054525637cd592b2a4b01") } > > db.demo765.insertOne( ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MySQL", ... "Marks":85 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054525637cd592b2a4b02") } > db.demo765.insertOne( ... { ... Name:"Chris", ... "Category":"10th", ... "SubjectName":"Math", ... "Marks":98 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054535637cd592b2a4b03") }
Find() পদ্ধতি -
এর সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করুন> db.demo765.find();
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে{ "_id" : ObjectId("5eb054525637cd592b2a4b01"), "Name" : "John", "Category" : "ComputerScience", "SubjectName" : "MongoDB", "Marks" : 75 } { "_id" : ObjectId("5eb054525637cd592b2a4b02"), "Name" : "John", "Category" : "ComputerScience", "SubjectName" : "MySQL", "Marks" : 85 } { "_id" : ObjectId("5eb054535637cd592b2a4b03"), "Name" : "Chris", "Category" : "10th", "SubjectName" : "Math", "Marks" : 98 }
গ্রুপ একাধিক ফলাফল -
একত্রিত করার জন্য নিম্নোক্ত ক্যোয়ারী> db.demo765.aggregate([ ... { "$facet": { ... "ListOfName": [ ... { "$group": { ... "_id": "$Name", ... "Marks": { "$sum": "$Marks" } ... }}, ... { "$project": { ... "_id": false, ... "Name": "$_id", ... "Marks": 1 ... }} ... ], ... "ListOfCategory": [ ... { "$group": { ... "_id": "$Category", ... "Marks": { "$sum": "$Marks" } ... }}, ... { "$project": { ... "_id": false, ... "Category": "$_id", ... "Marks": 1 ... }} ... ] ... }} ... ]).pretty()
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে{ "ListOfName" : [ { "Marks" : 98, "Name" : "Chris" }, { "Marks" : 160, "Name" : "John" } ], "ListOfCategory" : [ { "Marks" : 98, "Category" : "10th" }, { "Marks" : 160, "Category" : "ComputerScience" } ] }