Feature news

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 }


0 comments: