MongoDB CLI Tools

I have discussed one of the GUI tools MongoDB offers, Compass, previously. Sometimes, however, using the command line interface (CLI) is required. MongoDB provides some very helpful CLI tools. Let’s have a quick look at what is included in the MongoDB installation package, and what the tools and files do.

Package Components

There are two main types of files which come included in the MongoDB download, process/service files and tools. The process files are the core components of the MongoDB system include the following:

Process
  • mongod, which is the core database process
  • mongos, which controls and routes queries in a sharded environment
  • mongo is the interactive, JavaScript based, MongoDB shell.
Service

In the Windows download there are some additional files for running and configuring MongoDB as a Windows Service.

These are the main applications you will find yourself using most often to get the server up and running (mongod) and interacting with the server in the mongo shell (mongo). It is possible to get some server information with shell database methods such as serverStatus() and stats(). However, there are some CLI tools which offer much more detailed information for us.

CLI Tools

There are a couple of different buckets in which the CLI tools fall; import/export and diagnostic tools. Let’s take a closer look:

Import/Export Tools

As with most databases, having a way to bring data into and out of the database is extremely useful. MongoDB is no different. Being a document database doesn’t mean that it can’t provide a way to utilize structured data when needed. Or, to provide a way to export it’s rich document data for use elsewhere.

MongoDB stores data on disk in BSON format and allows for the importing (restoration) and exporting (dumping) of files in this format with the following CLI tools.

  • mongodump, generates a BSON file from a running mongod server.
  • mongorestore allows for the restoration of the files

There also in an included application, bsondump, which will convert the BSON dump files into JSON files. Recall that BSON is a binary form of JSON and includes some important data features such as data typing, such as date, integer, long, double, and decimal.

For working with formats other than BSON, MongoDB provides for support for importing and exporting data in JSON, CSV, or TSV format. These can be especially useful when bringing in established relational data of many types.

  • mongoimport brings the JSON, CSV, or TSV formatted data into a running MongoDB database
  • mongoexport, yep you guessed it, exports the database data.
CLI Tools for Diagnosis

This is where the real workhorses come in for examining the health of your MongoDB server environment. The provided tools allow for the examination of the current operation of a MongoDB server. One can also look at, and capture, network traffic or manage LDAP configurations.

  • mongostat is great for a obtaining the overview status of a running mongod or mongos instance. For example, it can provide information regarding the number of inserts, queries, updates, or deletes and lots more.

    Mongostats
    Mongostat results while doing large inserts
  • mongotop looks at, at a collection level, the time for reading and writing of data. It provides, at a high level, a view of where Mongodb is spending it’s time.

    mongotop
    mongotop results while doing inserts
  • mongoperf checks disk performance
  • mongoreplay is pretty cool. It allows for, among other things, the capture of commands sent to a MongoDB instance and the ability to replay them in a different environment. This is very handy for testing and trouble shooting.
  • mongoldap allows for testing LDAP configuration options against LDAP server(s).
Other Tools

The last tool which comes in the MongoDB package is mongofiles which allows for interaction with GridFS objects. GridFS allows for the storage of files larger than the BSON limit of 16MB per document.

Conclusion

Graphical User Interfaces (GUI) are great, but sometimes you will find it necessary to use a CLI tool to really understand what is going on with your system. MongoDB provides a great assortment of tools to do just that. I would recommend having a look at some of them to expand your personal tool-kit.


Follow me on Twitter @kenwalger to get the latest updates on my postings.

Facebooktwitterredditlinkedinmail

WVSE Meetup, Intro to NoSQL

I had the opportunity to speak recently at the monthly Willamette Valley Software Engineers (WVSE) Meetup (@WVSEmeetup) about NoSQL. I would like to thank the organizers of the event, Angelo Seminary (@angeloseminary of i.t.motives) and Brenton Paulson (@brentonpaulsen) for a great event. Mr. Paulson’s company, MAPS Credit Union offers a great venue. This was my second time attending an event at WVSE, the first as a speaker. Both visits have been great experiences, and very well coordinated.

Willamette Valley Software Engineers (WVSE)

Discussion Topic

I did a high-level overview of NoSQL and what some of the different models of databases look like in the NoSQL world. We then took a look at how MongoDB fits into the NoSQL space. I have a blog post about Why NoSQL to, take a look here. I also shared how to model data in documents.

In addition to the overview, I provided a brief example of CRUD operations in MongoDB. While the coverage of CRUD wasn’t too in depth, it was a good introduction. I have posts on CRUD with MongoDB using Python and Java for additional reading.

As NoSQL seemed to be a relatively new topic to most of the dozen or so people in attendance, there were some great questions asked. Hopefully, I was able to satisfactorily answer everyone’s questions. It was great to have Chase Swanson in attendance too as he was able to add his experience with MongoDB to the discussion.

About WVSE

If you are a software engineer or developer in the greater Willamette Valley area, I would encourage you to check WVSE out. They meet regularly to share ideas, learn from one another to network and meet others, and to have a good time! Regardless of your preferred technology or favorite languages or tools, they would like you to join them. Their format is to meet once a month with some casual networking, a presentation with Q and A, and maybe some after hours at a fun place to cap the evening. You can check out their upcoming meetings and topics on their Meetup page.

 

Facebooktwitterredditlinkedinmail