Philipp Hauer's Blog

Engineering Management, Java Ecosystem, Kotlin, Sociology of Software Development

MongoDB: Useful Development Tools and Snippets

Posted on May 8, 2019. Updated on Feb 5, 2020

I’m using MongoDB in production for many years. In this time, I tried different tools and development approaches; some turned out to be useful for us, others don’t. In this post, I like to share handy CLI tools for working with MongoDB, a Docker-based local development approach and helpful Mongo shell snippets.

MongoDB: Useful Development Tools and Snippets

TL;DR

  • CLI tools: - Mongo-Hacker - fzf for history search on steroids - Mongo-Monitor
  • Dockerized development setup powered by Docker-Compose - Container 1: MongoDB - Container 2: Mongo-Express to quickly view and edit the data in the browser - Container 3: Seeding script to initialize the MongoDB with randomized dummy data.
  • Useful Mongo shell commands: - Get a document by ID: db.products.find("5cc57fc685e5849abe2266c7") - Index Size: db.products.stats() - Index Usage: db.products.aggregate([{ $indexStats: {} }], { cursor: {} })
  • MongoDB desktop clients - MongoDB Compass - Studio 3T

Mongo Shell Enhancements with Mongo-Hacker

I highly recommend to install Mongo-Hacker. It provides neat enhancements for the Mongo Shell like syntax coloring and formatting by default (no need to append .pretty() anymore). There is much more to discover; just check out the documentation.

Syntax Coloring, Formatting, and Timing with Mongo-Hacker.

Syntax Coloring, Formatting, and Timing with Mongo-Hacker.

fzf-Powered Shell History Search

fzf is a really awesome command line tool for fuzzy-searching things like files and folders. But for me, the most powerful feature of fzf is to apply the fuzzy search to the shell history (via key bindings). When it comes to MongoDB, I use the fuzzy history search to quickly find the required mongo connection line to connect to a certain database. Just hit Ctrl-R and type “mongo databaseName/username/appName”.

Use fzf for the history search to quickly find the required mongo connection command

Use fzf for the history search to quickly find the required mongo connection command

Needless to say, that you should encrypt your disk when having passwords in the shell history file.

Mongo-Express, a MongoDB Web Client

Mongo-Express is a useful web client for MongoDB. It’s not a beauty, but useful to quickly take a look at some documents and change them while not having to configure a connection in a dedicated client. I like to use it during local development where I start it next to the MongoDB container with Docker-Compose. Check out the next section for details about this.

Mongo-Express: View Documents of a Collection

Mongo-Express: View Documents of a Collection

Mongo-Express: View and Edit a Document

Mongo-Express: View and Edit a Document

Local Development Setup with Docker-Compose

I’m a big fan of Docker-Compose for spinning up a local development environment with a database, seeding and useful tools.

Usually, the docker-compose.yml of my MongoDB projects looks like this:

version: '3'
services:
  mongo:
    image: "mongo:4.0.2"
    ports:
      - "27017:27017"
    command: --profile=1 --slowms=0
  mongo-seeding:
    build: ./local-dev/mongo-seeding/
    depends_on:
      - mongo
  mongo-express:
    image: "mongo-express:0.49.0"
    ports:
      - "8081:8081"
    depends_on:
      - mongo

Just call docker-compose up.

  • First, it starts a MongoDB by using the official MongoDB docker image.
  • Second, it runs a seeding script that populates the MongoDB with some nicely randomized dummy data. I like to use Python for this. The seeding script and Dockerfile can be found on GitHub.
  • Finally, it starts a Mongo-Express instance that points to mongo:27017 (that’s the default). For further configuration options see the image documentation on Docker Hub.

Mongo-Monitor

Mongo-Monitor is a really nice command line tool to visualize the setup and health of a MongoDB cluster. Check out the GitHub page for some screenshots.

Mongo Shell Commands

Find a Document By Id

We often look up a certain document by an Id. Fortunately, there is a nice shortcut syntax for this common use case.

# Don't:
db.products.find({ "_id": ObjectId("5cc57fc685e5849abe2266c7")})

# Do:
db.products.find("5cc57fc685e5849abe2266c7")

Show Index Sizes

db.products.stats()

Result:

{
      "indexSizes": {
            "_id_": 16384,
            "name_1": 16384
      }
}

Show Index Usage

After a MongoDB-based app has reached a certain age and size, it’s not so clear anymore which indexes are still used and which can be removed. Fortunately, there is a command for this:

db.products.aggregate( [ { $indexStats: { } } ], { cursor: {} } )

It shows how often an index was hit by a query and when this happened the last time. Mind that these statistics are reset after a restart of the MongoDB.

{
      "name": "name_1",
      "key": {
            "name": 1
      },
      "host": "653e83aa76c4:27017",
      "accesses": {
            "ops": NumberLong("150"),
            "since": ISODate("2019-04-28T10:26:14.864Z")
      }
}

MongoDB Desktop Clients: Compass and Studio 3T

I’m a command line fan. But there are also graphical desktop clients for MongoDB available:

  • MongoDB Compass. There is a restricted but free community edition and a unrestricted version that is part of a MongoDB subscription.
  • Studio 3T is a powerful commercial tool.

Both tools are useful when it comes to visualizing explain plans, visual aggregation building, index management, and in-place document editing. I really like to use a GUI for the visualization of the “executionStats” during performance optimizations and for building complex aggregation pipelines.