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

MicroPython’s WebREPL on the ESP8266

The NodeMCU ESP8266 device has built in WiFi. This allows us to, among other things, connect to the device through it’s own network to run commands. I walked through how connecting to a MicroPython enabled NodeMCU device is accomplished through the serial port in this post. I’d like to take a look now how to utilize another access feature that pairing MicroPython with an ESP8266 device allows, the WebREPL.

The WebREPL is a web based interface for the Read-Evaluate-Print-Loop (REPL) which allows for running commands on the ESP8266 in the same fashion as the terminal REPL does. However, since it is done through a wireless network connection it opens up some additional freedom for access. Of course having a device up and running on a network also poses some potential security issues, but we’ll look at those in another post.

Enable WebREPL

First thing first though, let’s assume that MicroPython is running on an ESP8266 device and you have a serial connection to the device. Again, have a look at my post here for that walk through. Assuming that you have flashed the ESP8266 with the latest version of MicroPython, version 1.8.7 as of this writing, the WebREPL must be enabled through the serial interface. It is a one line command to do from a command prompt:

import webrepl_setup

Entering in this command will produce a series of setup prompts, asking if you want WebREPL to be enabled at boot time, to set a password for the WebREPL, and to confirm rebooting the device to activate the changes.

Connect to Network

Once the system reboots you can connect to the device via WiFi. This is one of the neat features of these devices. If the board doesn’t connect to a wireless access point, it creates one for us. One can then connect to it via our computer or other wireless device. Look for an advertised WiFi network with a name of MicroPython-. The  will be a set of letters and numbers unique to each device.

Set Password

Upon selecting the device’s network you will need a password. The network password for the access point is micropythoN (note the capitalization of the last letter there). You should then be connected to the device’s network, congratulations!

Now in many situations one could simply browse to the device’s IP address in a browser and get some form of web page, such as our WebREPL interface. Remember that we are dealing with a device with limited memory here and, therefore, doesn’t have the actual webpage. Instead it provides a convenient websocket interface which can be used with the WebREPL Client. You can download it below:

Download MicroPython WebREPL Client

Once downloaded and unzipped you’ll want to open webrepl.html in either Chrome or Firefox (the browsers currently supported by WebREPL) You should see a screen similar to:

With your computer is connected to the device on the MicroPython network and using the default IP address and port of ws://192.168.4.1:8266 you can select the Connect button to start a WebREPL session.

You should see a prompt to enter your password, which is the one you set after enabling the WebREPL service on the device earlier. Once logged into the device you have access to the REPL just like you would from a serial connection.

Congratulations! You have just connected a NodeMCU ESP8266 to a wireless network with MicroPython. Not too horrible of a task, right?


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

Facebooktwitterredditlinkedinmail