dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes
>
# uery using sort()
Specifies the order in which the query returns matching documents. You must apply sort() to the cursor before retrieving any documents from the database. {1 : ascending order , -1 : descending order }
/* find all document from result collections and '_id' filed hide */
> db.result.find({},{_id:false})
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 5, "exam type" : "Exams", "score" : 55 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
Type "it" for more
/* find all document from result collections , '_id' filed hide and score is ascending order using sort function */
> db.result.find({},{_id:false}).sort({score:1})
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 9, "exam type" : "Quiz", "score" : 3 }
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 8, "exam type" : "Quiz", "score" : 14 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 12, "exam type" : "Exams", "score" : 20 }
{ "student" : 12, "exam type" : "Quiz", "score" : 21 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
Type "it" for more
/* find all document from result collections , '_id' filed hide and score is descending order using sort function */
> db.result.find({},{_id:false}).sort({score:-1})
{ "student" : 10, "exam type" : "Quiz", "score" : 93 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 16, "exam type" : "Essay", "score" : 87 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 17, "exam type" : "Quiz", "score" : 83 }
{ "student" : 11, "exam type" : "Essay", "score" : 79 }
{ "student" : 16, "exam type" : "Exams", "score" : 77 }
{ "student" : 14, "exam type" : "Essay", "score" : 76 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 20, "exam type" : "Exams", "score" : 75 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 11, "exam type" : "Exams", "score" : 71 }
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 15, "exam type" : "Essay", "score" : 69 }
{ "student" : 19, "exam type" : "Essay", "score" : 69 }
{ "student" : 19, "exam type" : "Quiz", "score" : 67 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
Type "it" for more
/* find all document from result collections , '_id' filed hide and (score , student ) are ascending order using sort function */
> db.result.find({},{_id:false}).sort({score:-1,student:-1})
{ "student" : 10, "exam type" : "Quiz", "score" : 93 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 16, "exam type" : "Essay", "score" : 87 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 17, "exam type" : "Quiz", "score" : 83 }
{ "student" : 11, "exam type" : "Essay", "score" : 79 }
{ "student" : 16, "exam type" : "Exams", "score" : 77 }
{ "student" : 14, "exam type" : "Essay", "score" : 76 }
{ "student" : 20, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 11, "exam type" : "Exams", "score" : 71 }
{ "student" : 19, "exam type" : "Essay", "score" : 69 }
{ "student" : 15, "exam type" : "Essay", "score" : 69 }
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 19, "exam type" : "Quiz", "score" : 67 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }
Type "it" for more
/* find all document from result collections , '_id' filed hide and student is descending order using sort function */
> db.result.find({},{_id:false}).sort({student:-1})
{ "student" : 20, "exam type" : "Exams", "score" : 75 }
{ "student" : 20, "exam type" : "Essay", "score" : 27 }
{ "student" : 20, "exam type" : "Quiz", "score" : 32 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }
{ "student" : 19, "exam type" : "Essay", "score" : 69 }
{ "student" : 19, "exam type" : "Quiz", "score" : 67 }
{ "student" : 18, "exam type" : "Exams", "score" : 42 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 18, "exam type" : "Quiz", "score" : 39 }
{ "student" : 17, "exam type" : "Exams", "score" : 29 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 17, "exam type" : "Quiz", "score" : 83 }
{ "student" : 16, "exam type" : "Exams", "score" : 77 }
{ "student" : 16, "exam type" : "Essay", "score" : 87 }
{ "student" : 16, "exam type" : "Quiz", "score" : 40 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Essay", "score" : 69 }
{ "student" : 15, "exam type" : "Quiz", "score" : 36 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 14, "exam type" : "Essay", "score" : 76 }
Type "it" for more
/* find all document from name collections and '_id' is descending order using sort function */
> db.name.find().sort({_id:-1})
{ "_id" : "Tula Steadman", "favoriteGame" : [ "football", "formula" ] }
{ "_id" : "Tonia Jefferis", "favoriteGame" : [ "basketball", "tennis" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [ "cricket", "NFS" ] }
{ "_id" : "Sebastian Kuo", "favoriteGame" : [ "NFS", "football" ] }
{ "_id" : "Refugia Kitts", "favoriteGame" : [ "formula", "pool" ] }
{ "_id" : "Pierre Ye", "favoriteGame" : [ "pool", "basketball" ] }
{ "_id" : "Petra Clarke", "favoriteGame" : [ "pool", "tennis" ] }
{ "_id" : "Pearlie Luther", "favoriteGame" : [ "formula", "pool" ] }
{ "_id" : "Nimishan", "favoriteGame" : [ "football", "cricket" ], "age" : 24 }
{ "_id" : "Neida Bandy", "favoriteGame" : [ "tennis", "hokey" ] }
{ "_id" : "Lazaro Izzi", "favoriteGame" : [ "basketball", "IPL" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [ "NFS", "football" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [ "football", "cricket" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [ "cricket", "formula" ] }
{ "_id" : "Jared Toenjes", "favoriteGame" : [ "hokey", "football" ] }
{ "_id" : "Janett Olivera", "favoriteGame" : [ "football", "basketball" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [ "cricket", "tennis" ] }
{ "_id" : "Gianna Kowaleski", "favoriteGame" : [ "NFS", "formula" ] }
{ "_id" : "Gene Smits", "favoriteGame" : [ "formula", "pool" ] }
{ "_id" : "Fana", "favoriteGame" : [ "IPL", "cricket" ], "age" : 23 }
Type "it" for more