Coder Social home page Coder Social logo

mongo-hacker's Introduction

MongoDB Shell Enhancements for Hackers

Warning

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.

Usage

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)

List of Enhancements

Basic

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

Disable notfication of "Type 'it' for more"

Custom prompt with hostname(process-version)> formating

Awesome

Colorized query output

Colorized Output

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

Highlight querytime if verboseShell is enabled

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

Colorized Quertime

IndexParanoia

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

Index Paranoia

Default indent is 2 spaces instead of tab

  • Customizable by setting __indent

AutoMulti

  • Automatically use multi updates -- to disable: setAutoMulti(false)
db.users.update({}, {$set: {awesome: true}})
Updated 4 existing record(s) in 1ms

Aggregation Framework Helpers -- on collections

  • Group and Count: gcount(group_field, filter)
  • Group and Sum: gsum(group_field, sum_field, filter)

Examples

Insert some data

db.users.insert([
  {
    "age": 27,
    "first_name": "Tyler",
    "last_name": "Brock",
    "updated": new Date()
  },
  { 
    "age": 30,
    "first_name": "Jessica",
    "last_name": "Fake",
    "updated": new Date()
  },
  {
    "age": 35,
    "first_name": "Tyler",
    "last_name": "Durden",
    "updated": new Date()
  }
])

Group and count users by the first_name field

db.users.gcount("first_name")
{
  "result": [
    {
      "_id": "Jessica",
      "count": 1
    },
    {
      "_id": "Tyler",
      "count": 2
    }
  ],
  "ok": 1
}

Group and count users having first_name of Tyler

db.users.gcount("first_name", {first_name: "Tyler"})
{
  "result": [
    {
      "_id": "Tyler",
      "count": 2
    }
  ],
  "ok": 1
}

Group users by first name and sum the age field

db.users.gsum("first_name", "age")
{
  "result": [
    {
      "_id": "Jessica",
      "sum": 30
    },
    {
      "_id": "Tyler",
      "sum": 62
    }
  ],
  "ok": 1
}

mongo-hacker's People

Contributors

abhas avatar puentesarrin avatar tylerbrock avatar

Watchers

 avatar  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.