Efficiently Perform Distinct with Multiple Keys in MongoDB



You can perform distinct with multiple keys with the help of an aggregate framework.

To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −

> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike","StudentAge":22,"StudentMathMarks":56});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7f74488d10a061296a3c53")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike","StudentAge":22,"StudentMathMarks":56});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7f744b8d10a061296a3c54")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":45});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7f74598d10a061296a3c55")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":45});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7f745e8d10a061296a3c56")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Carol","StudentAge":27,"StudentMathMarks":54});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7f74688d10a061296a3c57")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.distinctWithMultipleKeysDemo.find().pretty();

The following is the output −

{
   "_id" : ObjectId("5c7f74488d10a061296a3c53"),
   "StudentName" : "Mike",
   "StudentAge" : 22,
   "StudentMathMarks" : 56
}
{
   "_id" : ObjectId("5c7f744b8d10a061296a3c54"),
   "StudentName" : "Mike",
   "StudentAge" : 22,
   "StudentMathMarks" : 56
}
{
   "_id" : ObjectId("5c7f74598d10a061296a3c55"),
   "StudentName" : "Bob",
   "StudentAge" : 23,
   "StudentMathMarks" : 45
}
{
   "_id" : ObjectId("5c7f745e8d10a061296a3c56"),
   "StudentName" : "Bob",
   "StudentAge" : 23,
   "StudentMathMarks" : 45
}
{
   "_id" : ObjectId("5c7f74688d10a061296a3c57"),
   "StudentName" : "Carol",
   "StudentAge" : 27,
   "StudentMathMarks" : 54
}

Here is the query to perform distinct with multiple keys −

> c = db.distinctWithMultipleKeysDemo;
test.distinctWithMultipleKeysDemo
> myResult = c.aggregate( [ {"$group": { "_id": { StudentName:"$StudentName", StudentAge: "$StudentAge" } } } ] );

The following is the output −

{ "_id" : { "StudentName" : "Carol", "StudentAge" : 27 } }
{ "_id" : { "StudentName" : "Bob", "StudentAge" : 23 } }
{ "_id" : { "StudentName" : "Mike", "StudentAge" : 22 } }
Updated on: 2019-07-30T22:30:25+05:30

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements