┌───────────────────────────────────────────────────────────────────────────────────┐ │ __ ___ __ __ __ │ │ / |/ /___ ____ ____ _____ / / / /___ ______/ /_____ _____ │ │ / /|_/ / __ \/ __ \/ __ `/ __ \ / /_/ / __ `/ ___/ //_/ _ \/ ___/ │ │ / / / / /_/ / / / / /_/ / /_/ / / __ / /_/ / /__/ ,< / __/ / │ │ /_/ /_/\____/_/ /_/\__, /\____/ /_/ /_/\__,_/\___/_/|_|\___/_/ │ │ /____/ │ └───────────────────────────────────────────────────────────────────────────────────┘
MongoDB Shell Enhancements
- These enhancements are useful to me but they don't make sense for everyone. Feel free to tweak to your desire and please submit feedback or pull requests.
- Does not work with
mongoshell or MongoDB servers < 2.4
- Updates called on existing cursors are new and experimental (see notes in API section)
npm install -g mongo-hacker
- Sort document keys by default
- Highlight querytime if verboseShell is enabled
- In green if querytime is at or below slowms
- In red if query time is above slowms
- Default indent is 2 spaces instead of tab
- Customizable by setting
indentkey of config
- Customizable by setting
- Verbose shell is enabled by default -- to disable:
- Disable notfication of "Type 'it' for more"
- Custom prompt:
- Always pretty print. You can still use default format by appending
.ugly()to the end of db statement.
- Show DBs has aligned columns, is sorted by database name and shows less significant digits (in master for Mongo 2.5/2.6)
sh.status()output (remove lastmod, take up less space, colorize chunk's shard)
- Colorized query output for console/terminal windows supporting ANSI color codes.
Additional shell commands
To make interactive use of the MongoDB shell even more convenient,
mongo-hacker adds the following shell commands:
count tables: count the number of collections in each of the mongo server's databases - by @pvdb
count docs: count the number of documents in all (non-
system) collections in the database - by @pvdb
count indexes: list all collections and display the size of all indexes - by @cog-g
Some of these commands have hidden features that can be enabled in the
mongo-hacker config, to make the command output even more useful:
- by changing the
count documentscommand will also print out the change in the number of documents since the last count - by @pvdb
Get a list of database names: (by @pvdb)
(note that this method is similar - functionality-wise and usage-wise - to the existing
db.getCollectionNames() API method and allows for advanced, cross-database scripting in the MongoDB shell)
Filter for a collection of documents:
One for finding a single document:
dbcollection == dbcollection
Select for selecting fields to return (projection):
Reverse for descending sort by insertion order (default) or arbitrary field:
Last for finding last inserted document (default) or document last by given field:
Update, Replace, Upsert and Remove can be called on a DBQuery Object:
dbcollection // multi updatedbcollection // single replacementdbcollection // single upsertdbcollection // multi remove
Sort, limit, and skip through multi updates and removes:
Note: The performance of multi updates involving a skip or limit may be worse than those without those specifications due to there being absolutely no native support for this feature in MongoDB itself. It should be understood by the user of this software that use of this feature (by calling update on a cursor rather than a collection) is advanced and experimental. The option to do this sort of operation is purely additive to the MongoDB experience with MongoHacker and usage of it is in no way required. Furthermore, its inclusion in this enhancement does not effect the operation of updates invoked through collections and, in practice, is insanely useful.
The aggregation framework is now fluent as well. You can use it as currently documented or via the chainable methods.
Calling aggregate without an array of operations or $operations will make it a match.
// matches every documentdbcollectiondbcollection// matches documents where the "a" is equal to 1dbcollection// matches documents where "a" is greater than 7dbcollection
Additional methods can then be chained on top of the inital match in order to make more complicated aggregations.
// Match and projectdbcollectiondbcollection// Match, group and sortdbcollectiondbtest
For easy and simple random data generation you can utilise these methods below. You can use any of these functions in a loop. For example:
// Inserts 20 documents with random data.for i=1; i<21; i++dbcollection;
You can specify the length of each word, the number of words, and an optional seeded word in a sentence randomly. Use the optional
seed parameter for testing text search.
randomWord(length=5, words=1, seed=undefined)
// Inserts a random sentence consisting of 5 letters per word, 5 words in total,// with a probability to insert the word 'needle' in the sentencedbcollection// Inserts a random word consisting of 16 lettersdbcollection
You can specify maximum number to be randomly generated (exclusive)
// Inserts a random number in the range of 0 or 1.dbcollection// Inserts a random number in the range of 0 or 999.dbcollection
You can specify start and end dates range to be randomly generated. (exclusive)
randomDate(start= <2 years ago> , end=Date() )
// Inserts a random date object in the range of 1st January 2016 to 1st February 2016dbcollection// If today is 19th May 2016 and you specify only the start of the day,// this will generate random date object between 00:00:00 to current time.dbcollection
General Shell Helpers
findCommand('search')list commands that match the search string
Aggregation Framework Helpers -- on collections
- Group and Count:
- Group and Sum:
gsum(group_field, sum_field, filter)
- Group and Average:
gavg(group_field, avg_field, filter)
Run function on some/all databases