use test_db;
db.test_db.insertOne({country:'A', p_name:'AA', p_lang:'AL1'});
db.test_db.insertOne({country:'A', p_name:'AB', p_lang:'AL1'});
db.test_db.insertOne({country:'A', p_name:'AC', p_lang:'AL2'});
db.test_db.insertOne({country:'A', p_name:'AD', p_lang:'AL1'});
db.test_db.insertOne({country:'B', p_name:'BA', p_lang:'AL1'});
db.test_db.insertOne({country:'B', p_name:'BB', p_lang:'AL1'});
db.test_db.insertOne({country:'B', p_name:'AC', p_lang:'AL2'});
db.test_db.insertOne({country:'C', p_name:'CC', p_lang:'AL2'});
db.test_db.insertOne({country:'C', p_name:'AC', p_lang:'AL2'});
db.test_db.insertOne({country:'C', p_name:'AD', p_lang:'AL2'});
db.test_db.insertOne({country:'C', p_name:'CD', p_lang:'AL2'});
#Show all documents in collection.
> db.test_db.find();
{ "_id" : ObjectId("5a73452883af1d0a6b9b1c45"), "country" : "A", "p_name" : "AA", "p_lang" : "AL1" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c47"), "country" : "A", "p_name" : "AB", "p_lang" : "AL1" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c48"), "country" : "A", "p_name" : "AC", "p_lang" : "AL2" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c49"), "country" : "A", "p_name" : "AD", "p_lang" : "AL1" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c4a"), "country" : "B", "p_name" : "BA", "p_lang" : "AL1" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c4b"), "country" : "B", "p_name" : "BB", "p_lang" : "AL1" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c4c"), "country" : "B", "p_name" : "AC", "p_lang" : "AL2" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c4d"), "country" : "C", "p_name" : "CC", "p_lang" : "AL2" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c4e"), "country" : "C", "p_name" : "AC", "p_lang" : "AL2" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c4f"), "country" : "C", "p_name" : "AD", "p_lang" : "AL2" }
{ "_id" : ObjectId("5a73468183af1d0a6b9b1c50"), "country" : "C", "p_name" : "CD", "p_lang" : "AL2" }
#group on multiple fields and Sort on multiple resulting fields
> db.test_db.aggregate([{"$group":{_id:{country:"$country", p_lang: "$p_lang"}, count:{$sum:1}} }, {"$sort": {"_id.country":1, "_id.p_lang":1}}]);
{ "_id" : { "country" : "A", "p_lang" : "AL1" }, "count" : 3 }
{ "_id" : { "country" : "A", "p_lang" : "AL2" }, "count" : 1 }
{ "_id" : { "country" : "B", "p_lang" : "AL1" }, "count" : 2 }
{ "_id" : { "country" : "B", "p_lang" : "AL2" }, "count" : 1 }
{ "_id" : { "country" : "C", "p_lang" : "AL2" }, "count" : 4 }
#Sort on count field.
> db.test_db.aggregate([{"$group":{_id:{country:"$country", p_lang: "$p_lang"}, count:{$sum:1}} }, {"$sort": {"_id.country":1, "count":-1}}]);
{ "_id" : { "country" : "A", "p_lang" : "AL1" }, "count" : 3 }
{ "_id" : { "country" : "A", "p_lang" : "AL2" }, "count" : 1 }
{ "_id" : { "country" : "B", "p_lang" : "AL1" }, "count" : 2 }
{ "_id" : { "country" : "B", "p_lang" : "AL2" }, "count" : 1 }
{ "_id" : { "country" : "C", "p_lang" : "AL2" }, "count" : 4 }
#Selected fields only in result.. note first object is filter criteria.
> db.test_db.find({}, {country:1, _id:0, p_lang:1});
{ "country" : "A", "p_lang" : "AL1" }
{ "country" : "A", "p_lang" : "AL1" }
{ "country" : "A", "p_lang" : "AL2" }
{ "country" : "A", "p_lang" : "AL1" }
{ "country" : "B", "p_lang" : "AL1" }
{ "country" : "B", "p_lang" : "AL1" }
{ "country" : "B", "p_lang" : "AL2" }
{ "country" : "C", "p_lang" : "AL2" }
{ "country" : "C", "p_lang" : "AL2" }
{ "country" : "C", "p_lang" : "AL2" }
{ "country" : "C", "p_lang" : "AL2" }