Feature news

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 }


0 comments: