06. MongoDB - CRUD operations - select the fields using $gt,$gte,$lt,$lte
Download file
# our database-UNIVERSITY , collection-RESULT
first run mongo server (mongod)
open new terminal import json file
download file copy (db_university.json) to paste c:mongoDB\bin
>mongoimport -d <db name> -c <collection name> < <file path/file name.json>
>mongoimport -d university -c result < db_university.json
> db.result.find()
{ "_id" : ObjectId("538de41d2110740e7c9a5373"), "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5375"), "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "_id" : ObjectId("538de41d2110740e7c9a5376"), "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
....
* gt -- greater than
* gte -- greater than equal
* lt -- less than
* lte -- less than equal
# query using $gt
> db.result.find({"score":{$gt:59}},{"_id":false,"student":true,"score":true})
{ "student" : 1, "score" : 69 }
{ "student" : 1, "score" : 92 }
{ "student" : 2, "score" : 75 }
{ "student" : 2, "score" : 66 }
{ "student" : 4, "score" : 74 }
{ "student" : 7, "score" : 92 }
{ "student" : 8, "score" : 85 }
{ "student" : 9, "score" : 61 }
{ "student" : 10, "score" : 93 }
....
# query using $gte
> db.result.find({"score":{$gte:59}},{"_id":false,"student":true,"score":true})
{ "student" : 1, "score" : 69 }
{ "student" : 1, "score" : 92 }
{ "student" : 2, "score" : 75 }
{ "student" : 2, "score" : 66 }
{ "student" : 2, "score" : 59 }
{ "student" : 4, "score" : 74 }
{ "student" : 6, "score" : 59 }
.....
# query using $lt
> db.result.find({"score":{$lt:59}},{"_id":false,"student":true,"score":true})
{ "student" : 1, "score" : 54 }
{ "student" : 3, "score" : 12 }
{ "student" : 3, "score" : 15 }
{ "student" : 3, "score" : 19 }
{ "student" : 4, "score" : 56 }
{ "student" : 4, "score" : 38 }
{ "student" : 5, "score" : 55 }
{ "student" : 5, "score" : 24 }
{ "student" : 5, "score" : 24 }
....
# query using $lte
> db.result.find({"score":{$lte:59}},{"_id":false,"student":true,"score":true})
{ "student" : 1, "score" : 54 }
{ "student" : 2, "score" : 59 }
{ "student" : 3, "score" : 12 }
{ "student" : 3, "score" : 15 }
{ "student" : 3, "score" : 19 }
{ "student" : 4, "score" : 56 }
{ "student" : 4, "score" : 38 }
{ "student" : 5, "score" : 55 }
{ "student" : 5, "score" : 24 }
....
Download file
download file copy (name.json) to paste c:mongoDB\bin
# query using $gt
> db.name.find({"_id":{$gt:"G"}},{"_id":true})
{ "_id" : "Gene Smits" }
{ "_id" : "Gianna Kowaleski" }
{ "_id" : "Gisele Melancon" }
{ "_id" : "Janett Olivera" }
{ "_id" : "Jared Toenjes" }
{ "_id" : "Jeanine Pigott" }
{ "_id" : "Jene Goding" }
....
# query using $lt
> db.name.find({"_id":{$lt:"G"}},{"_id":true})
{ "_id" : "Alessandra Helton" }
{ "_id" : "Alta Oxner" }
{ "_id" : "Bambi Geraci" }
{ "_id" : "Brigida Hertzog" }
{ "_id" : "Carey Carriere" }
{ "_id" : "Carisa Topham" }
{ "_id" : "Daina Mclendon" }
....
0 comments: