Installing the GUI-based client, MongoVUE, for MongoDB

In this recipe, we will look at a GUI-based client for MongoDB. Throughout the book, we have used the Mongo shell to perform various operations we need. Its advantages are as follows:

  • It comes packaged with the MongoDB installation
  • Being lightweight, you need not worry about it taking up your system's resources
  • On servers where GUI-based interfaces are not present, the shell is the only option to connect, query, and administer the server instance

Having said this, if you are not on a server and want to connect to a database instance to query, view the plan of a query, administer, and so on, it is nice to have a GUI with these features to let you do things at a click of a button. As a developer, we always query our relational database with a GUI-based thick client; so why not do the same for MongoDB?

In this recipe, we will see how to install some features of a MongoDB client, MongoVUE. This client is only available on Windows machines. This product has both a paid version (with various levels of licensing per number of users) and a free version that has some limitations. For this recipe, we'll look at the free version.

Getting ready

For this recipe, the following steps are necessary:

  1. Start a single instance of the MongoDB server. The port on which the connections are accepted will be the default one, 27017.
  2. Import the following two collections from the command prompt after the MongoDB server is started:
    $ mongoimport --type json personTwo.json -c personTwo -d test --drop
    $ mongoimport --type csv -c postalCodes -d test pincodes.csv --headerline --drop
    

How to do it…

  1. Download the installer ZIP for the MongoVUE from http://www.mongovue.com/downloads/. Once downloaded, it is a matter of a few clicks and the software gets installed.
  2. Open the installed application. As this is a free version, we will have all the features available for the first 14 days, after which some of the features will not be available. The details of this can be seen at http://www.mongovue.com/purchase/.
  3. The first thing we will do is add a database connection as follows:
    1. Once the following window is opened, click on the + button to add a new connection.
      How to do it…
    2. Once opened, we will get another window in which we will fill in the server-connection details. Fill in the following details in the new window and click on Test. This should succeed if the connection works. Finally, click on Save, as shown in the following screenshot:
      How to do it…
    3. Once added, connect to the instance.
  4. In the left-hand-side navigation panel, we will see the instances added and the databases in them, as shown in the following screenshot:
    How to do it…

    As we see in the preceding screenshot, hovering the mouse over the name of the collection shows us the size and count of the documents in the collection

  5. Let's see how to query a collection and get all the documents. We will use the postalCodes collection for our test. Right-click on the collection name and then click on View. We will see the contents of the collection shown as Tree View, where we can expand and see the contents; Table View, which shows the contents in a tabular grid; or Text View, which shows the contents as normal JSON text.
  6. Let's see what happens when we query a collection with nested documents; personTwo is a collection with the following sample document in it:
    {
      "_id" : 1,
      "_class" : "com.packtpub.mongo.cookbook.domain.Person2",
      "firstName" : "Steve",
      "lastName" : "Johnson",
      "age" : 30,
      "gender" : "Male",
      "residentialAddress" : {
        "addressLineOne" : "20, Central street",
        "city" : "Sydney",
        "state" : "NSW",
        "country" : "Australia"
      }
    }
    
  7. When we query to see all the documents in the collection, we will see the following screenshot:
    How to do it…
  8. The residentialAddress column shows that the value is a nested document with the given number of fields present in it. Hovering your mouse over it shows the nested document. Alternatively, you can click on the column to show the contents in this document again as a grid. Once the nested document(s) are shown, you can click on the top of the grid to come back one level.

Let's see how to write queries to retrieve the selected documents:

  1. Right-click on the postalCodes collection and click on Find. We will type the following query in the {Find } textbox and in the {Sort} field, and click on the Find button:
    How to do it…
  2. We can choose from the tab the type of view we want, such as Tree View, Table View, or Text View. The plan of the query is also shown. Whenever any operation is run, the Learn shell at the bottom shows the actual Mongo query executed. In this case, we will see the following query:
    [ 11:17:07 PM ]
    db.postalCodes.find({ "city" : /Mumbai/i }).limit(50);
    db.postalCodes.find({ "city" : /Mumbai/i }).limit(50).explain();
    
  3. The plan of a query is also shown every time and, as of the current version 1.6.9.0, there is no way to disable the setting that shows the query plan with the query.
  4. In the Tree View tab, right-clicking on a document will give you more options such as expanding it, copying the JSON contents, adding keys to this document, and removing the document. Try to remove a document from this collection with a right-click and also try adding any additional keys to the document.
  5. You might choose to restore the documents by reimporting the data from the postalCodes collection.

To insert a document in the collection, perform the following steps. We will insert a document in the personTwo collection.

  1. Right-click on the personTwo collection name and click on Insert/Import Documents…, as shown in the following screenshot:
    How to do it…
  2. Another pop-up window will come up where you can choose to enter a single JSON document or a valid text file with JSON documents to be imported. We will import the following document by importing a single document:
    {
      "_id" : 4,
      "firstName" : "Jack",
      "lastName" : "Jones",
     "age" : 35,
     "gender" : "Male"
    }
  3. Query the collection once the document is imported successfully; we will see the newly imported document along with the old ones.

Let's see how to update the document:

  1. You can either right-click on the collection name on the left-hand side and click on Update or select the Update option at the top. In either case, we will have the following window. Here, we will update the age of the person we inserted in the previous step as follows:
    How to do it…

    Some things to note in this GUI are the query textbox on the left-hand side to find the document to be updated, and the update JSON on the right-hand side, which will be applied to the selected document(s).

  2. Before you update, you might choose to hit the Count button to see the number of documents that can be updated (in this case, one). On clicking on Find, we can see the documents in the tree form. On the right-hand side, below the update JSON text, we have the option to update one document and multiple documents by clicking on Update 1 or Update All, respectively.
    • You might choose whether the Upsert operation is to be done or not if the document(s) for the given find condition are not found
    • The radio buttons in the bottom-right corner of the screen, as shown in the preceding screenshot, either show the output of the getLastError operation or the result after update; in this case, a query will be executed to find the document(s) updated
    • However, the find query is not foolproof and might return different results from those truly updated as a separate query, the same as in the {Find} textbox, and the update and find operations are not atomic

We have queried on small collections so far. As the size of the collection increases, queries that perform full collection scans are not acceptable, and we need to create indexes as follows:

  1. To create an index by lastName in the ascending order and age in the descending order for instance, we will invoke db.personTwo.ensureIndex({'lastName':1, 'age':-1}).
  2. Using MongoVUE, there is a way to visually create an index by right-clicking on the collection name on the left-hand side of the screen and selecting Add Index….
  3. In the new pop-up window, enter the name of the index and select the Visual tab, as shown in the following screenshot. Select the lastName and age fields with the Ascending (1) and Descending (-1) values, respectively.
    How to do it…
  4. Once the preceding details are filled in, click on Create. This will create the index for us by firing the ensureIndex command, as we can see in the Learn Shell below.
  5. You can opt for the index to be unique and drop duplicates (which will be enabled when Unique is selected) or even create big, long-running index creations in the background. Note the Json tab next to the Visual tab. That is the place where you can type in the ensureIndex command as you do from the shell to create the index.

Now, we will see how to drop an index:

  1. Simply expand the tree on the left-hand side.
  2. On expanding the collection, we will see all the indexes created on it.
  3. Except for the default index on the _id field, all other indexes can be dropped.
  4. Simply right-click on the name and select Drop index to drop or click on Properties to view its properties.

After seeing how to do the basic CRUD operations and after creating the index, let's look at how to execute the aggregation operations.

  1. There are no visual tools such as index creation in case of aggregation but simply a text area where we can enter our aggregation pipeline. In the following sample, we will perform aggregation on the postalCodes collection to find the top five states by the number of times they appear in the collection:
    {'$project' : {'state':1, '_id':0}},
    {'$group': {'_id':'$state', 'count':{'$sum':1}}},
    {'$sort':{'count':-1}},
    {'$limit':5}
    
  2. We will have the following aggregation pipeline entered:
    How to do it…
  3. Once the pipeline is entered, hit the Aggregate button to get the aggregation results.

Executing MapReduce is even cooler. The use case that we will execute is similar to the one we used earlier, but we will see how to implement the MapReduce operation using MongoVUE:

  1. To execute a MapReduce job, right-click on the collection name in the left-hand-side menu and click on Map Reduce.
  2. This option is right above the Aggregate option, as seen in the previous screenshot. This gives us a pretty neat GUI to enter the Map, Reduce, Finalize, and In & Out options, as shown in the following screenshot:
    How to do it…
  3. The Map function is simply as follows:
    function Map() {
      emit(this.state, 1)
    }
  4. The Reduce function is as follows:
    function Reduce(key, values) {
      return Array.sum(values)
    }
  5. Leave the Finalize method unimplemented and in the In & Out section, fill in the following details:
    How to do it…
  6. Click on Go to start executing the MapReduce job.
  7. We will have the output to the mongoVue_mr collection. Query the mongoVue_mr collection using the following query:
    > db.mongoVue_mr.find().sort({value:-1}).limit(5)
    
  8. Verify the results against those we got using aggregation. The format of MapReduce was chosen as Reduce. For more options and their behavior, visit http://docs.mongodb.org/manual/reference/command/mapReduce/#mapreduce-out-cmd.

Monitoring the server instances is now easily possible using MongoVUE. To do this, perform the following steps:

  1. To monitor an instance, navigate to Tools | Monitoring in the top menu.
  2. By default, no server will be added, and we will have to click on + Add Server to add a server instance.
  3. Select the local instance added or any server you want to monitor and click on Connect.
  4. We will see quite a lot of monitoring details. MongoVUE uses the db.serverStatus command to serve these stats. Thus, to limit the frequency at which we execute this command on busy server instances, we can choose Refresh Interval at the top of the screen, as shown in the following screenshot:
    How to do it…

How it works…

What we covered in the previous sections was pretty straightforward; using this information, we can perform the majority of our activities as a developer and administrator.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset