MongoDB usage notes

MongoDB Shell commands:

#list dbs on server:
show dbs
#show collections in current db
show collections
#switch to db
use databasename;

#find everything and print as json
db.collectionname.find().forEach(printjson);

#'like' query:
db.collectionname.find(fieldname:/pattern/).forEach(printjson);

#count results:
db.collectionname.find().count();

#return only requested properties (similar to 'select x, y, z from table a')
db.collectionname.find({}, {fieldname1:1, fieldname2:1}).forEach(printjson)

#select min x by sorting in asc order and picking first result
db.collectionname.find().sort({x: 1}).limit(1).forEach(printjson)

#select max x by sorting in desc order and picking first result
db.collectionname.find().sort({x: -1}).limit(1).forEach(printjson)

#add an index for property x
db.collectionname.ensureIndex({x: 1})

Stats webapp: http://localhost:28017/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.