cardoni
2/25/2015 - 12:14 AM

Useful Mongo Shell Queries

Useful Mongo Shell Queries

// Returns an Array of all Mongo Collections
db.getCollectionNames();

// Returns Each Mongo Collection w/ Count
db.getCollectionNames().forEach( function ( col ) { print( col, db.getCollection( col ).stats()[ 'count' ] ) } );

// Regex Searching ( w/ Pretty Printing )
db.<collection_name_here>.find( { name: { $regex: /^regex_to_match_on_here/i } } ).pretty();

// Most-Recently-Added Record in Collection ( w/ Pretty Printing )
db.<collection_name_here>.find().sort( { _id: -1 } ).limit( 1 ).pretty();