From 42557db56d6ac61d7320c4b8cf0ca44aa1270624 Mon Sep 17 00:00:00 2001 From: Joshua Maserow Date: Wed, 13 Apr 2022 18:28:17 +0200 Subject: [PATCH 1/5] 'Added javascript_keys option, so that objects will be displayed in JavaScript notation (mongosh style), rather than JSON, meaning that { _id:1, name:bob, height:180 } is displayed like this { _id:1, name:bob, height:180 } which is easier for humans to read.' --- config.js | 2 +- hacks/common.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/config.js b/config.js index ac28e18..ddfad62 100644 --- a/config.js +++ b/config.js @@ -15,6 +15,7 @@ mongo_hacker_config = { enhance_api: true, // additonal api extensions indent: 2, // number of spaces for indent sort_keys: false, // sort the keys in documents when displayed + javascript_keys:false, // output is formatted with JavaScript style keys uuid_type: 'default', // 'java', 'c#', 'python' or 'default' banner_message: 'Mongo-Hacker ', // banner message version: '0.1.1', // current mongo-hacker version @@ -53,4 +54,3 @@ mongo_hacker_config = { if (mongo_hacker_config['show_banner']) { print(mongo_hacker_config['banner_message'] + mongo_hacker_config['version']); } - diff --git a/hacks/common.js b/hacks/common.js index 2f2cf9c..6016874 100644 --- a/hacks/common.js +++ b/hacks/common.js @@ -249,7 +249,8 @@ tojsonObject = function( x, indent, nolint, nocolor, sort_keys ) { continue; var color = mongo_hacker_config.colors.key; - s += indent + colorize("\"" + key + "\"", color, nocolor) + ": " + tojson( val, indent , nolint, nocolor, sortKeys ); + var formattedKey = mongo_hacker_config.javascript_keys && key.match(/^[A-Za-z_][A-Za-z\d_]*$/) ? key : '"' + key + '"'; + s += indent + colorize(formattedKey, color, nocolor) + ": " + tojson( val, indent , nolint, nocolor, sortKeys ); if (num != total) { s += ","; num++; From 5f74851de00e428113b7bd4bc7a2d1624d8a5229 Mon Sep 17 00:00:00 2001 From: Joshua Maserow Date: Wed, 13 Apr 2022 18:47:01 +0200 Subject: [PATCH 2/5] 'Added javascript_keys to README.md' --- README.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index af87de1..986884c 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ mongo - Default indent is 2 spaces instead of tab (config: `indent`) - Disable notification of "Type 'it' for more" - Option to sort document keys (config: `sort_keys`) + - Option to format keys in JavaScript style (config: `javascript_keys`) - Custom prompt: `hostname(process-version)[rs_status:set_name] db>` - Always pretty print. You can still use default format by appending `.ugly()` to the end of a statement. - Colorized query output for console/terminal windows supporting ANSI color codes. @@ -160,29 +161,29 @@ db.test.aggregate().group({_id: '$a', 'sum': {'$sum': 1}}).sort({sum: -1}) #### Data Generation -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: +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: ```js -// Inserts 20 documents with random data. -for (i=1; i<21; i++) { +// Inserts 20 documents with random data. +for (i=1; i<21; i++) { db.collection.insert( { - word: randomWord(), - number: randomNumber(), - date: randomDate() + word: randomWord(), + number: randomNumber(), + date: randomDate() } - ); + ); } ``` -##### randomWord +##### randomWord 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)` +`randomWord(length=5, words=1, seed=undefined)` ```js -// Inserts a random sentence consisting of 5 letters per word, 5 words in total, +// Inserts a random sentence consisting of 5 letters per word, 5 words in total, // with a probability to insert the word 'needle' in the sentence db.collection.insert( { words: randomWord(5, 5, 'needle') } ) @@ -197,15 +198,15 @@ You can specify maximum number to be randomly generated (exclusive) `randomNumber(max=100)` ```js -// Inserts a random number in the range of 0 or 1. +// Inserts a random number in the range of 0 or 1. db.collection.insert( { number: randomNumber(2) } ) -// Inserts a random number in the range of 0 or 999. +// Inserts a random number in the range of 0 or 999. db.collection.insert( { number: randomNumber(1000) } ) ``` -##### randomDate +##### randomDate You can specify start and end dates range to be randomly generated. (exclusive) @@ -215,7 +216,7 @@ You can specify start and end dates range to be randomly generated. (exclusive) // Inserts a random date object in the range of 1st January 2016 to 1st February 2016 db.collection.insert( { date: randomDate(ISODate("2016-01-01T00:00:00"), ISODate("2016-02-01T00:00:00")) }) -// If today is 19th May 2016 and you specify only the start of the day, +// 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. db.collection.insert( { date: randomDate(ISODate("2016-05-19T00:00:00")) }) ``` From cd1fae6eb5bdd3477e683660d477b8055235a603 Mon Sep 17 00:00:00 2001 From: Joshua Maserow Date: Mon, 6 Jun 2022 16:00:58 +0200 Subject: [PATCH 3/5] 'Updated README.md for master branch.' --- README.md | 256 +----------------------------------------------------- 1 file changed, 3 insertions(+), 253 deletions(-) diff --git a/README.md b/README.md index 986884c..f61e961 100644 --- a/README.md +++ b/README.md @@ -1,255 +1,5 @@ +# Wrong branch - ┌───────────────────────────────────────────────────────────────────────────────────┐ - │ __ ___ __ __ __ │ - │ / |/ /___ ____ ____ _____ / / / /___ ______/ /_____ _____ │ - │ / /|_/ / __ \/ __ \/ __ `/ __ \ / /_/ / __ `/ ___/ //_/ _ \/ ___/ │ - │ / / / / /_/ / / / / /_/ / /_/ / / __ / /_/ / /__/ ,< / __/ / │ - │ /_/ /_/\____/_/ /_/\__, /\____/ /_/ /_/\__,_/\___/_/|_|\___/_/ │ - │ /____/ │ - └───────────────────────────────────────────────────────────────────────────────────┘ +**This (master) branch is useless. -# MongoDB Shell Enhancements - -## Warnings - -* 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](https://github.com/TylerBrock/mongo-hacker/issues). -* Only tested with non-EOL versions of MongoDB server (currently 3.4+) -* Does not work with `mongo` shell or MongoDB servers < 2.4 -* Updates called on existing cursors are experimental (see notes in API section) - -## Installation - -```sh -npm install -g mongo-hacker -mongo -``` - - -#### Install from source -``` -git clone https://github.com/TylerBrock/mongo-hacker -cd mongo-hacker -make install -cd .. -rm -rdf mongo-hacker/ -mongo -``` - -## Enhancements - -#### Basic UX - - - Verbose shell is enabled by default (config: `verbose_shell`) - - To toggle temporarily run `setVerboseShell(false)` - - Highlight query time if verbose shell is enabled - - In **green** if query time is at or below slowms - - In **red** if query time is above slowms - - Default indent is 2 spaces instead of tab (config: `indent`) - - Disable notification of "Type 'it' for more" - - Option to sort document keys (config: `sort_keys`) - - Option to format keys in JavaScript style (config: `javascript_keys`) - - Custom prompt: `hostname(process-version)[rs_status:set_name] db>` - - Always pretty print. You can still use default format by appending `.ugly()` to the end of a statement. - - Colorized query output for console/terminal windows supporting ANSI color codes. - ![Colorized Output](http://tylerbrock.github.com/mongo-hacker/screenshots/colorized_shell.png) - -#### Additional shell commands - -The MongoDB shell offers various "shell commands" _(sometimes referred to as "shell helpers" as well)_ that make interactive use of the shell much more convenient than [proper, Javascript-only scripted use of the shell][interactive_versus_scripted]. - -To make interactive use of the MongoDB shell even more convenient, `mongo-hacker` adds the following shell commands: - -* `count collections`/`count tables`: count the number of collections in each of the mongo server's databases -* `count documents`/`count docs`: count the number of documents in all _(non-`system`)_ collections in the database -* `count indexes`: list all collections and display the size of all indexes - -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_deltas` setting to `true` in `config.js`, the `count documents` command will also print out the change in the number of documents since the last count - -[interactive_versus_scripted]: http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/#differences-between-interactive-and-scripted-mongo - -#### API Additions - -##### Scripting - -Get a list of database names: - -```js -db.getMongo().getDatabaseNames() -``` - -_(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)_ - -##### General - -One for finding a single document: - -```js -db.collection.find({ ... }).one() == db.collection.findOne({ ... }) -``` - -Select for selecting fields to return (projection): - -```js -db.collection.find({ ... }).select({ name: 1 }) -``` - -Reverse for descending sort by insertion order (default) or arbitrary field: - -```js -db.collection.find({ ... }).reverse() -db.collection.find({ ... }).reverse('createDate') -``` - -Last for finding last inserted document (default) or document last by given field: - -```js -db.collection.find({ ... }).last() -db.collection.find({ ... }).last('createDate') -``` - -Update, Replace, Upsert and Remove can be called on a DBQuery Object: - -```js -db.collection.find({ ... }).update({ ... }) // multi update -db.collection.find({ ... }).replace({ ... }) // single replacement -db.collection.find({ ... }).upsert({ ... }) // single upsert -db.collection.find({ ... }).remove() // multi remove -``` - -Sort, limit, and skip through multi updates and removes: - -```js -db.collection.find({ ... }).limit(7).update({ ... }) -db.collection.find({ ... }).sort({ ... }).skip(1).limit(3).update({ ... }) -db.collection.find({ ... }).limit(3).remove() -``` - -**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.* - - -#### Aggregation Framework - -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. - -```js -// matches every document -db.collection.aggregate() -db.collection.aggregate({}) - -// matches documents where the "a" is equal to 1 -db.collection.aggregate({a: 1}) - -// matches documents where "a" is greater than 7 -db.collection.aggregate({a: {$gt: 7}}) -``` - -Additional methods can then be chained on top of the inital match in order to make more complicated aggregations. - -```js -// Match and project -db.collection.aggregate().project() -db.collection.aggregate({a: 1}).project({a: 1, _id: 0}) - -// Match, group and sort -db.collection.aggregate({}).group({}).sort({}) -db.test.aggregate().group({_id: '$a', 'sum': {'$sum': 1}}).sort({sum: -1}) -``` - -#### Data Generation - -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: - -```js -// Inserts 20 documents with random data. -for (i=1; i<21; i++) { - db.collection.insert( - { - word: randomWord(), - number: randomNumber(), - date: randomDate() - } - ); -} -``` - -##### randomWord - -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)` - -```js -// Inserts a random sentence consisting of 5 letters per word, 5 words in total, -// with a probability to insert the word 'needle' in the sentence -db.collection.insert( { words: randomWord(5, 5, 'needle') } ) - -// Inserts a random word consisting of 16 letters -db.collection.insert( { words: randomWord(16) } ) -``` - -##### randomNumber - -You can specify maximum number to be randomly generated (exclusive) - -`randomNumber(max=100)` - -```js -// Inserts a random number in the range of 0 or 1. -db.collection.insert( { number: randomNumber(2) } ) - -// Inserts a random number in the range of 0 or 999. -db.collection.insert( { number: randomNumber(1000) } ) - -``` - -##### randomDate - -You can specify start and end dates range to be randomly generated. (exclusive) - -`randomDate(start= <2 years ago> , end=Date() )` - -```js -// Inserts a random date object in the range of 1st January 2016 to 1st February 2016 -db.collection.insert( { date: randomDate(ISODate("2016-01-01T00:00:00"), ISODate("2016-02-01T00:00:00")) }) - -// 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. -db.collection.insert( { date: randomDate(ISODate("2016-05-19T00:00:00")) }) -``` - - -#### Helpers - -General Shell Helpers - - - `findCommand('search')` list commands that match the search string - - -Aggregation Framework Helpers -- on collections - - - Group and Count: `gcount(group_field, filter)` - - Group and Sum: `gsum(group_field, sum_field, filter)` - - Group and Average: `gavg(group_field, avg_field, filter)` - -Run function on some/all databases - -```js -runOnDbs(/db_names_regexp/, function(db) { - // callback is ran for each database which name matches regular expression - // db is that selected database -}); -``` - -## Recent Changes - -See [CHANGELOG.md](CHANGELOG.md) for a list of changes from previous versions of Mongo Hacker. - -A very special thanks to all of the [contributors to Mongo Hacker](https://github.com/TylerBrock/mongo-hacker/graphs/contributors). - -## Disclaimer - -This software is not supported by [MongoDB, Inc.](https://www.mongodb.com/) under any of their commercial support subscriptions or otherwise. Any usage of Mongo Hacker is at your own risk. Bug reports, feature requests, and questions can be posted in the [Issues section](https://github.com/TylerBrock/mongo-hacker/issues?q=is%3Aopen+is%3Aissue) on GitHub. +Please go to [npmjsorg/release branch](https://github.com/softwarecreations/mongo-hacker/tree/npmjsorg/release) From b7f28f665761255832105c744f3a2a8f7000b5c4 Mon Sep 17 00:00:00 2001 From: Joshua Maserow Date: Mon, 6 Jun 2022 16:02:43 +0200 Subject: [PATCH 4/5] 'Updated README.md for master branch again.' --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index f61e961..a07aff8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ # Wrong branch -**This (master) branch is useless. - Please go to [npmjsorg/release branch](https://github.com/softwarecreations/mongo-hacker/tree/npmjsorg/release) From 3342cfb5e2a180b8c6de3cad203fdaab174a1da4 Mon Sep 17 00:00:00 2001 From: softwarecreations Date: Wed, 8 Jun 2022 15:58:09 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a07aff8..9483ee3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Wrong branch -Please go to [npmjsorg/release branch](https://github.com/softwarecreations/mongo-hacker/tree/npmjsorg/release) +Please go to my [npmjsorg/release branch](https://github.com/softwarecreations/mongo-hacker/tree/npmjsorg/release)