Giter VIP home page Giter VIP logo

mongo-hacker's Introduction

MongoDB Shell Enhancements for Hackers

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 pull requests.
  • Does not work in Windows (currently)
  • Does not work with shells or db servers < 2.2 (currently)
  • Updates called on existing cursors are new and experimental (see notes in API secion)

Installation

Link mongo_hacker.js to .mongorc.js in your home directory:

ln -sf <mongo-hacker-dir>/mongo_hacker.js ~/.mongorc.js

Note: This currently only works with the v2.2+ of shell (which you can use with earlier versions of the server safely)

Enhancements

Basic UX

Highlight querytime if verboseShell is enabled

  • In green if querytime is at or below slowms
  • In red if query time is above slowms

IndexParanoia

  • Automatically show information about index use -- to disable: setIndexParanoia(false)

Default indent is 2 spaces instead of tab

  • Customizable by setting __indent

Verbose shell is enabled by default -- to disable: setVerboseShell(false)

Disable notfication of "Type 'it' for more"

Custom prompt

hostname(process-version)[rs status] db>

API

Filter for a collection of documents:

db.collection.filter(<criteria>)

One for finding a single document:

db.collection.filter({ ... }).one() == db.collection.findOne({ ... })

Select for selecting fields to return (projection):

db.collection.filter({ ... }).select({ name: 1 })

Reverse for descending sort by insertion order (default) or arbitrary field:

db.collection.filter({ ... }).reverse()
db.collection.filter({ ... }).reverse('createDate')

Last for finding last inserted document (default) or document last by given field:

db.collection.filter({ ... }).last()
db.collection.filter({ ... }).last('createDate')

Update, Replace, Upsert and Remove can be called on a DBQuery Object:

db.collection.filter({ ... }).update({ ... })  // multi update
db.collection.filter({ ... }).replace({ ... }) // single replacement
db.collection.filter({ ... }).upsert({ ... })  // single upsert
db.collection.filter({ ... }).remove()         // multi remove

Sort, limit, and skip through multi updates and removes:

db.collection.filter({ ... }).limit(7).update({ ... })
db.collection.filter({ ... }).sort({ ... }).skip(1).limit(3).update({ ... })
db.collection.filter({ ... }).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 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)

Colorization

Colorized query output

Colorized Output

  • ObjectId: Green(underlined)
  • null: Bright Red
  • String: Green
  • Number: Red
  • Key: Yellow
  • Boolean: Blue
  • Date: Cyan

mongo-hacker's People

Contributors

abhas avatar bwaldvogel avatar osyvokon avatar puentesarrin avatar tylerbrock avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.