Feature news

Showing posts with label mongoDB. Show all posts

14. MongoDB - CRUD operations - update the fileds using $set , $unset




dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes

/*  find the all document from info collection  */
> db.info.find()
{ "_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }

/*  find the document '_id' equal 'fahimfana' from info collection  */
> db.info.find({ "_id" : "fahimfana"}).pretty()
{
"_id" : "fahimfana",
"age" : 22,
"email" : {
"personal" : "mohamedfahim91@gmail.com",
"offcial" : "fahimfana91@gmail.com"
},
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
}
}

/*  update document from '_id' equal 'fahimfana'  ==> this is wrong way because erase other details  can be use $set command  */
> db.info.update({_id:"fahimfana"},{age:18})
>
> db.info.find({_id:"fahimfana"})
{ "_id" : "fahimfana", "age" : 18 }
> db.info.find()
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "fahimfana", "age" : 18 }

> db.info.update({_id:"fahimfana"},{locatin:"Srilanka"})
> db.info.find({_id:"fahimfana"})
{ "_id" : "fahimfana", "locatin" : "Srilanka" }

/*  insert a document '_id':'fahimfana'   */
> db.info.insert({"_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } })

> db.info.find({ "_id" : "fahimfana"}).pretty()
{
 "_id" : "fahimfana",
 "age" : 22,
 "email" : {
  "personal" : "mohamedfahim91@gmail.com",
  "offcial" : "fahimfana91@gmail.com"
 },
 "address" : {
  "street" : "hettuwewa",
  "city" : "anuradapura",
  "country" : "srilanka"
 }
}

#  Query using $set()
Use the $set operator to replace the value of a field to the specified value. If the field does not exist, the $set operator will add the field with the specified value.

> db.info.update({_id:"fahimfana"},{$set:{age:18}})
>
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"age" : 18,
"email" : {
"personal" : "mohamedfahim91@gmail.com",
"offcial" : "fahimfana91@gmail.com"
},
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
}
}
> db.info.update({_id:"fahimfana"},{$set:{"email.business":"fahimPvt@gmail.com"}})
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
},
"age" : 18,
"email" : {
"business" : "fahimPvt@gmail.com",
"offcial" : "fahimfana91@gmail.com",
"personal" : "mohamedfahim91@gmail.com"
}
}

#  Query using $unset()
The $unset operator deletes a particular field. The specified value in the $unset expression (i.e. "" below) does not impact the operation.

> db.info.update({_id:"fahimfana"},{$unset:{"email.business":"fahimPvt@gmail.com"}})
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
},
"age" : 18,
"email" : {
"offcial" : "fahimfana91@gmail.com",
"personal" : "mohamedfahim91@gmail.com"
}
}
>


Learn more »

13. MongoDB - CRUD operations - Part_III_Querying , Coursor {sort()}



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


Learn more »

12. MongoDB - CRUD operations - Part_II_Querying , Coursor {skip(),limit()}


#  limit()
Use the limit() method on a cursor to specify the maximum number of documents the cursor will return

#  skip()
Call the skip() method on a cursor to control where MongoDB begins returning results. This approach may be useful in implementing “paged” results.

> 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 }
{ "_id" : ObjectId("538de41d2110740e7c9a5379"), "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "_id" : ObjectId("538de41d2110740e7c9a537a"), "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "_id" : ObjectId("538de41d2110740e7c9a537b"), "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "_id" : ObjectId("538de41d2110740e7c9a537c"), "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "_id" : ObjectId("538de41d2110740e7c9a537e"), "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "_id" : ObjectId("538de41d2110740e7c9a537f"), "student" : 5, "exam type" : "Exams", "score" : 55 }
{ "_id" : ObjectId("538de41d2110740e7c9a5380"), "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5381"), "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5382"), "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "_id" : ObjectId("538de41d2110740e7c9a5383"), "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "_id" : ObjectId("538de41d2110740e7c9a5384"), "student" : 6, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a5385"), "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "_id" : ObjectId("538de41d2110740e7c9a5386"), "student" : 7, "exam type" : "Essay", "score" : 5 }
Type "it" for more

/* hide '_id' filed  and find all documents */
> db.result.find({_id:false})
> 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

/* hide '_id' filed  and display the first 5 documents */
> db.result.find({},{_id:false}).limit(5)
{ "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 }

/* hide '_id' filed  and skip first 3 documents */
> db.result.find({},{_id:false}).skip(3)
{ "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 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 8, "exam type" : "Exams", "score" : 40 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
Type "it" for more

/* hide '_id' filed  and skip first 3 document  display the 5 documents (4,5,6,7,8)*/
> db.result.find({},{_id:false}).skip(3).limit(5)
{ "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 }

/* hide '_id' filed  and select 'score' lessthan equal 20 */
> db.result.find({score:{$lte:20}},{_id:false})
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
{ "student" : 8, "exam type" : "Quiz", "score" : 14 }
{ "student" : 9, "exam type" : "Quiz", "score" : 3 }
{ "student" : 12, "exam type" : "Exams", "score" : 20 }
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }

/* hide '_id' filed  , select 'score' lessthan equal 20 and count all document */
> db.result.find({score:{$lte:20}},{_id:false}).count()
18

/* hide '_id' filed  , select 'score' lessthan equal 20 and skip 10 documents */
> db.result.find({score:{$lte:20}},{_id:false}).skip(10)
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }

/* hide '_id' filed  , select 'score' lessthan equal 20 and skip 10 documents  limit 3 documents*/
> db.result.find({score:{$lte:20}},{_id:false}).skip(10).limit(3)
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }


Learn more »

11. MongoDB - CRUD operations - Part_I_Querying , Coursor {count()}


dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes

#  count()
Counts the number of documents in a collection. Returns a document that contains this count and as well as the command status

/* count all document */
> db.name.find().count()
31
> db.name.count()
31
> db.name.find({}).count()
31
> db.name.count({})
31
/* count the document  '_id' value start with first letter 'A'  */   
> db.name.find({_id:{$regex:"^A"}}).count()
2
> db.name.count({_id:{$regex:"^A"}})
2
> db.name.find({_id:{$regex:"^A"}})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }

/* find the all document */   
> db.name.find({})
{ "_id" : "Tonia Jefferis", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Pearlie Luther", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Pierre Ye", "favoriteGame" : [  "pool",  "basketball" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Erminia Kubala", "favoriteGame" : [  "IPL",  "pool" ] }
{ "_id" : "Petra Clarke", "favoriteGame" : [  "pool",  "tennis" ] }
{ "_id" : "Bambi Geraci", "favoriteGame" : [  "IPL",  "formula" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Dion Spradlin", "favoriteGame" : [  "pool",  "NFS" ] }
{ "_id" : "Enola Robert", "favoriteGame" : [  "formula",  "IPL" ] }
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Lazaro Izzi", "favoriteGame" : [  "basketball",  "IPL" ] }
{ "_id" : "Sebastian Kuo", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [  "cricket",  "formula" ] }
{ "_id" : "Gene Smits", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Janett Olivera", "favoriteGame" : [  "football",  "basketball" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
Type "it" for more

/* count the document  '_id' value start with first letter 'A' and 'favoriteGame' equal 'NFS' */  
 > db.name.find({_id:{$regex:"^A"},"favoriteGame":"NFS"})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
> db.name.find({_id:{$regex:"^A"},"favoriteGame":"NFS"}).count()
1
> db.name.count({_id:{$regex:"^A"},"favoriteGame":"NFS"})
1

/* count the all document */  
> db.result.find().count()
60
> db.result..count()
60

/* count the document  'score' value graterthan equal '90'*/  
> db.result.find({score:{$gte:90}}).count()
3
> db.result.count({score:{$gte:90}})
> db.result.find({score:{$gte:90}})
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5387"), "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5390"), "student" : 10, "exam type" : "Quiz", "score" : 93 }
> db.result.find({score:{$gte:90}},{_id:false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 10, "exam type" : "Quiz", "score" : 93 }

/* count the document  'score' value graterthan equal '92' and 'exam type' equal 'Essay'*/  
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false}).count()
1
> db.result.count({score:{$gte:90},"exam type":"Essay"},{_id:false})
1                                                                                                                                               
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false,student:true})
{ "student" : 1 }
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false,student:false})
{ "exam type" : "Essay", "score" : 92 }


Learn more »

09. MongoDB - CRUD operations - select the fields using $in, $all


dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
name
result
system.indexes

/* find all document from name collection  */
> db.name.find()
{ "_id" : "Tonia Jefferis", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Pearlie Luther", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Pierre Ye", "favoriteGame" : [  "pool",  "basketball" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Erminia Kubala", "favoriteGame" : [  "IPL",  "pool" ] }
....

/* select 'favoriteGame' in value 'IPL' from name collection  */
> db.name.find({"favoriteGame":"IPL"})
{ "_id" : "Erminia Kubala", "favoriteGame" : [  "IPL",  "pool" ] }
{ "_id" : "Bambi Geraci", "favoriteGame" : [  "IPL",  "formula" ] }
{ "_id" : "Enola Robert", "favoriteGame" : [  "formula",  "IPL" ] }
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Lazaro Izzi", "favoriteGame" : [  "basketball",  "IPL" ] }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }


/* 'favoriteGame' in value is inside of array  */

#  query using $or
The $in operator selects the documents where the value of a field equals any value in the specified array

/* select 'favoriteGame' in value is 'cricket' using '$in' function from name collection  */
> db.name.find({"favoriteGame":{$in:["cricket"]}})
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [  "cricket",  "formula" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }
{ "_id" : "Carey Carriere", "favoriteGame" : [  "cricket",  "hokey" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }

/* select 'favoriteGame' in value is 'cricket','football' using '$in' function from name collection  */
> db.name.find({"favoriteGame":{$in:["cricket","football"]}})
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Sebastian Kuo", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [  "cricket",  "formula" ] }
{ "_id" : "Janett Olivera", "favoriteGame" : [  "football",  "basketball" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jared Toenjes", "favoriteGame" : [  "hokey",  "football" ] }
{ "_id" : "Carey Carriere", "favoriteGame" : [  "cricket",  "hokey" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
{ "_id" : "Tula Steadman", "favoriteGame" : [  "football",  "formula" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }

> db.name.find({"favoriteGame":{$in:["cricket","football","hokey"]}})
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Sebastian Kuo", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [  "cricket",  "formula" ] }
{ "_id" : "Janett Olivera", "favoriteGame" : [  "football",  "basketball" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Neida Bandy", "favoriteGame" : [  "tennis",  "hokey" ] }
{ "_id" : "Jared Toenjes", "favoriteGame" : [  "hokey",  "football" ] }
{ "_id" : "Carey Carriere", "favoriteGame" : [  "cricket",  "hokey" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
{ "_id" : "Tula Steadman", "favoriteGame" : [  "football",  "formula" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }

#  query using $all
The $all operator selects the documents where the value of a field is an array that contains all the specified element

/* select 'favoriteGame' in value is 'cricket' and 'football' using '$all' function from name collection  */
> db.name.find({"favoriteGame":{$all:["cricket","football"]}})
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }

/* select 'favoriteGame' in value is 'cricket' and 'IPL' using '$all' function from name collection  */
> db.name.find({"favoriteGame":{$all:["cricket","IPL"]}})
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }


Learn more »

10. MongoDB - CRUD operations - select the fields using dot notation



dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes
>

/* insert three document for 'db' =>uiversity and 'collection' => info  */
> db.info.insert({ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } })

> db.info.insert({ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } })

> db.info.insert({ "_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } })

/* find the all document from info collection  */
> db.info.find()
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }
{ "_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } }

/* select 'email' filed from info collection  */
> db.info.find({"email" : { "personal" : "mohamedzajith@uojonline.net"}})

> db.info.find({"email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com"}})
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }

/* select 'email' filed from info collection use pretty method */
> db.info.find({"email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com"}}).pretty()
{
"_id" : "mohamedzajith",
"age" : 24,
"email" : {
"personal" : "mohamedzajith@uojonline.net",
"offcial" : "mohamedzajith@gmail.com"
},
"address" : {
"street" : "campus lane",
"city" : "jaffna",
"country" : "srilanka"
}
}

/* select 'email' filed from info collection using dot notation   */
> db.info.find({"email.personal" : "mohamedzajith@uojonline.net"})
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }

> db.info.find({"email.personal" : "mohamedzajith@uojonline.net"}).pretty()
{
"_id" : "mohamedzajith",
"age" : 24,
"email" : {
"personal" : "mohamedzajith@uojonline.net",
"offcial" : "mohamedzajith@gmail.com"
},
"address" : {
"street" : "campus lane",
"city" : "jaffna",
"country" : "srilanka"
}
}

Learn more »

08. MongoDB - CRUD operations - select the fields using $or,$and

dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
name
result
system.indexes

>

#  query using $or
The $or operator performs a logical OR operation on an array of two or more <expressions> and selects the documents that satisfy at least one of the <expressions>

/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$or' function */
> db.result.find({$or:[{"exam type":"Essay"},{score:{$gte:24}}]})
{ "_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 }
{ "_id" : ObjectId("538de41d2110740e7c9a537a"), "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "_id" : ObjectId("538de41d2110740e7c9a537c"), "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
....

/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$or' function and hide '_id' filed */
> db.result.find({$or:[{"exam type":"Essay"},{score:{$gte:24}}]},{"_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 }
....

#  query using $and 
$and performs a logical AND operation on an array of two or more expressions (e.g. <expression1>, <expression2>, etc.) and selects the documents that satisfy all the expressions in the array

/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$and' function  */ > db.result.find({$and:[{"exam type":"Essay"},{score:{$gte:24}}]})
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "_id" : ObjectId("538de41d2110740e7c9a5380"), "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5389"), "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "_id" : ObjectId("538de41d2110740e7c9a538c"), "student" : 9, "exam type" : "Essay", "score" : 61 }
....


/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$and' function and hide '_id' filed */
> db.result.find({$and:[{"exam type":"Essay"},{score:{$gte:24}}]},{"_id":false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
....


Learn more »

07 - MongoDB - CRUD operations - select the fields using $regex,$exists,$type


#  our database-UNIVERSITY ,  collection-NAME

insert into this two documents
> db.name.insert({"_id":"Fana","favoriteGame":["IPL","cricket"],"age":23})
> db.name.insert({"_id":"Nimishan","favoriteGame":["football","cricket"],"age":24})
> db.name.find({})
.....
{ "_id" : "Brigida Hertzog", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
{ "_id" : "Tula Steadman", "favoriteGame" : [  "football",  "formula" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }

#  query using $exists
> db.name.find({"age":{$exists:true}})
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }

> db.name.find({"age":{$exists:false}})
{ "_id" : "Tonia Jefferis", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Pearlie Luther", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Pierre Ye", "favoriteGame" : [  "pool",  "basketball" ] }
....

#  query using $type
$type selects the documents where the value of the field is the specified BSON type
TypeNumber
Double1
String2
Object3
find more types....
> db.name.find({"age":{$type:1}})
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }
> db.name.find({"age":{$type:2}})

> db.name.find({"_id":{$type:1}})

> db.name.find({"_id":{$type:2}})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }
{ "_id" : "Bambi Geraci", "favoriteGame" : [  "IPL",  "formula" ] }
{ "_id" : "Brigida Hertzog", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Carey Carriere", "favoriteGame" : [  "cricket",  "hokey" ] }
....

#  query using $regex
The $regex operator provides regular expression capabilities for pattern matching strings in queries
> db.name.find({"_id":{$regex:"ss"}})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
> db.name.find({"_id":{$regex:"na"}})
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }
{ "_id" : "Gianna Kowaleski", "favoriteGame" : [  "NFS",  "formula" ] }

find first character
> db.name.find({"_id":{$regex:"^D"}})
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Dion Spradlin", "favoriteGame" : [  "pool",  "NFS" ] }
> db.name.find({"_id":{$regex:"^Da"}})
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }

find last character
> db.name.find({"_id":{$regex:"on$"}})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
> db.name.find({"_id":{$regex:"n$"}})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Dion Spradlin", "favoriteGame" : [  "pool",  "NFS" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Tula Steadman", "favoriteGame" : [  "football",  "formula" ] }


Learn more »

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" }
....


Learn more »

05. MongoDB - CRUD operations - select all fields


                                                                                                     Download file
our database-UNIVERSITY ,  collection-RESULT

#  select single field
> 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 }
....

> db.result.find({"student":2})
{ "_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 }

> db.result.find({"exam type":"Quiz"})
{ "_id" : ObjectId("538de41d2110740e7c9a5375"), "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a537b"), "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "_id" : ObjectId("538de41d2110740e7c9a537e"), "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "_id" : ObjectId("538de41d2110740e7c9a5381"), "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5384"), "student" : 6, "exam type" : "Quiz", "score" : 59 }
....

#  select double or more field

> db.result.find({"student":2,"exam type":"Quiz"})
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz","score":59})
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a5384"), "student" : 6, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz","score":59},{"_id":false})
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz","score":59},{"_id":false,"student":true})
{ "student" : 2 }
{ "student" : 6 }

> db.result.find({"exam type":"Quiz","score":59},{"_id":false,"student":true,"score":true})
{ "student" : 2, "score" : 59 }
{ "student" : 6, "score" : 59 }


Next ===> MongoDB - CRUD operations query using field SELECT graterhtan,lessthan


Learn more »

04. MongoDB - CRUD operations FIND


# create database name (university) and collection name (result)
>use university
switched to db university
>db.createCollection("result")
{ "ok" : 1 }
>
>for (i=1; i<21; i++) { "name"=["Essay","Exams","Quiz"]; for (j=0; j<3; j++){ db.result.insert( {"student":i , "exam type":name[j] , "score":Math.round(Math.random()*100) }) ; } } 
>

# if you need to display all the document. use for find() method                       db.<collection name>.find()  

>db.result.find()
{ "_id" : ObjectId("538dd52e2110740e7c9a534b"), "studrnt" : 1, "type" : "Exams", "score" : 70 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534c"), "studrnt" : 1, "type" : "Essay", "score" : 5 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534d"), "studrnt" : 2, "type" : "Exams", "score" : 80 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534e"), "studrnt" : 2, "type" : "Essay", "score" : 83 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534f"), "studrnt" : 3, "type" : "Exams", "score" : 42 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5350"), "studrnt" : 3, "type" : "Essay", "score" : 36 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5351"), "studrnt" : 4, "type" : "Exams", "score" : 67 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5352"), "studrnt" : 4, "type" : "Essay", "score" : 28 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5353"), "studrnt" : 5, "type" : "Exams", "score" : 31 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5354"), "studrnt" : 5, "type" : "Essay", "score" : 7 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5355"), "studrnt" : 6, "type" : "Exams", "score" : 23 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5356"), "studrnt" : 6, "type" : "Essay", "score" : 32 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5357"), "studrnt" : 7, "type" : "Exams", "score" : 94 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5358"), "studrnt" : 7, "type" : "Essay", "score" : 79 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5359"), "studrnt" : 8, "type" : "Exams", "score" : 6 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535a"), "studrnt" : 8, "type" : "Essay", "score" : 28 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535b"), "studrnt" : 9, "type" : "Exams", "score" : 9 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535c"), "studrnt" : 9, "type" : "Essay", "score" : 74 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535d"), "studrnt" : 10, "type" : "Exams", "score" : 4 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535e"), "studrnt" : 10, "type" : "Essay", "score" : 79 }
Type "it" for more
>

# if you need to display the results in a formatted way. you can use pretty()    method db.<collection name>.find() .pretty() 

>db.result.find().pretty()
"_id" : ObjectId("533a5661c49df1c1c35cec92"), 
"student" : 1, 
"exam type" : "Essay", 
"score" : 95
}
{
"_id" : ObjectId("533a5661c49df1c1c35cec93"), 
"student" : 1, 
"exam type" : "Exams", 
"score" : 43 
}
"_id" : ObjectId("533a5661c49df1c1c35cec94"), 
"student" : 1, 
"exam type" : "Quiz", 
"score" : 6 
}
"_id" : ObjectId("533a5661c49df1c1c35cec95"), 
"student" : 2, 
"exam type" : "Essay", 
"score" : 54 
}
...
...
>

# if you need to display the first results in a formatted way. you can use findOne() method db.<collection name>.findOne() 

>db.result.findOne()
"_id" : ObjectId("533a5661c49df1c1c35cec92"), 
"student" : 1, 
"exam type" : "Essay", 
"score" : 95 
}
>

Next ===> mongoDB - CRUD operations find() operator and operations

Learn more »