MongoDB

dealing with the javascript console

Connect and show dbs

# mongo localhost:27017/eoleaaf

show collections

> help
db.help()                    help on db methods
db.mycoll.help()             help on collection methods
sh.help()                    sharding helpers
rs.help()                    replica set helpers
help admin                   administrative help
help connect                 connecting to a db help
help keys                    key shortcuts
help misc                    misc things to know
help mr                      mapreduce

show dbs                     show database names
show collections             show collections in current database
show users                   show users in current database
show profile                 show most recent system.profile entries with time >= 1ms
show logs                    show the accessible logger names
show log [name]              prints out the last segment of log in memory, 'global' is default
use <db_name>                set current database
db.foo.find()                list objects in collection foo
db.foo.find( { a : 1 } )     list objects in foo where a == 1
it                           result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x   set default number of items to display on shell
exit                         quit the mongo shell
> show dbs
admin              (empty)
local              0.078GB
mongoengine_test   0.078GB
mongoengine_test2  0.078GB
mongoengine_test3  0.078GB
pymongo_test       0.078GB
pymongo_test2      (empty)
>

drop database:

> db.dropDatabase()
{ "dropped" : "aaf", "ok" : 1 }

Find an entry

> db.user.findOne()
db.user.findOne({UserType:"eleve"})
  • find an entry where a field exists:

    db.user.find({"ENTEleveClasses":{$exists:true}})
    
  • find an entry with a specific subfield:

    > db.user.find({"ENTEleveClasses":{$exists:true}}, {"ENTEleveClasses.name":1})
    { "_id" : ObjectId("59bfde67b92f3b1e9659cad9"), "ENTEleveClasses" : { "name" :
    "T STL" } }
    { "_id" : ObjectId("59bfde67b92f3b1e9659cada"), "ENTEleveClasses" : { "name" :
    "T STL" } }
    { "_id" : ObjectId("59bfde67b92f3b1e9659cadb"), "ENTEleveClasses" : { "name" :
    "T STL" } }
    

and filter it:

> db.user.find({"UserType":"eleve", "ENTEleveClasses.name":"T STL"},
{ENTPersonLogin:1})
{ "_id" : ObjectId("59bfde67b92f3b1e9659cad9"), "ENTPersonLogin" :
"albertine.delacourt02" }
{ "_id" : ObjectId("59bfde67b92f3b1e9659cada"), "ENTPersonLogin" :
"albertine.delacourt03" }
{ "_id" : ObjectId("59bfde67b92f3b1e9659cadb"), "ENTPersonLogin" :
"albertine.delacourt04" }

Using groups

> db.user.aggregate([   { $match: {"FieldActionType" : "CREATE"}},    {
$group: {       _id: null,        count: {         $sum: 1       }     }   }
]);
{ "_id" : null, "count" : 13 }
> db.user.aggregate([   { $match: {"FieldActionType" : "CREATE"}},    {
$group: {       _id: "$FieldActionType",        count: {         $sum: 1
}     }   } ]);
{ "_id" : "CREATE", "count" : 13 }

> db.user.aggregate([{      $group: {       _id: "$ENTEleveClasses"}   } ]);
> db.user.aggregate([{      $group: {       _id: "$ENTEleveClasses",
count: {         $sum: 1       }     }   } ]);

> db.user.aggregate([{$match:{"UserType":"eleve"}}, {      $group: {
_id: "$ENTEleveClasses"}   } ]);
{ "_id" : { "etablissement" : ObjectId("59bfde67b92f3b1e9659cad0"), "name" :
"T STL" } }
{ "_id" : { "etablissement" : ObjectId("59bfde67b92f3b1e9659cacf"), "name" :
"T STL" } }

> db.user.aggregate([{$match:{"UserType":"eleve"}}, {      $group: {
_id: "$ENTEleveClasses.name"}   } ]);
{ "_id" : "T STL" }