Installing a 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 that we need. Its advantages are as follows:

  • It comes packaged with the MongoDB installation
  • Being lightweight, you don't need to worry about it taking up your system's resources
  • On servers where GUI-based interfaces are not present, 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 in the click of a button. As a developer, we always query our relational database with a GUI-based thick client, so why not for MongoDB?

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

Getting ready

For this recipe, the following steps are necessary:

  1. Start a single instance of 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 mongod server has 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 that we will do is add a database connection:
    • Once the following window has opened, click on the (+) button to add a new connection:
      How to do it…
    • 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.
      How to do it…
    • Once added, connect to the instance.
  4. In the left navigation panel, we will see the instances added and the databases in them, as shown in the following image:
    How to do it…

    As we can see in the preceding image, 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 click on View. We will see the contents of the collection shown as either a Tree View, where we can expand and see the contents, Table View, which shows the contents in a tabular grid, and 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"
      }
    }

    When we query to see all the documents in the collection, we see the following image:

    How to do it…

    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 documents are shown, you can click on the top of the grid to come back one level.

  7. Let's see how to write queries to retrieve selected documents:
    • Right-click on the postalCodes collection, and click on Find. We will type the following query in the {Find} textbox and the {Sort} field, and click on the Find button to the right:
      How to do it…
    • We can choose the type of view that we want from the tab, which is a 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 see the following:
      [ 11:17:07 PM ]
      db.postalCodes.find({ "city" : /Mumbai/i }).limit(50);
      db.postalCodes.find({ "city" : /Mumbai/i }).limit(50).explain();
      
    • 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 showing of the query plan with the query.
  8. In Tree View, right-clicking on a document will give you more options, such as expand it, copy the JSON contents, add keys to this document, remove the document, and so on. Try to remove a document from this collection using a right-click, and try adding any additional keys to the document. You can choose to restore the documents by reimporting the data from the postalCodes collection.
  9. To insert a document in the collection, perform the following. We will be inserting a document in the personTwo collection:
    • Right-click on the personTwo collection name, and click on Insert/Import Documents…, as shown in the following screenshot:
      How to do it…
    • Another pop-up window will appear, where you can choose to enter a single JSON document or valid text file with the JSON documents to be imported. We imported the following document by importing a single document:
      {
        "_id" : 4, 
        "firstName" : "Jack",
        "lastName" : "Jones",
       "age" : 35,
       "gender" : "Male" 
      }
    • Query the collection once the document has been imported successfully; we will view the newly imported document along with the old ones.
  10. Let's see how to update the document:
    • You can either right-click on the collection name to the left and click Update, or select the Update option at the top. In either case, we will see the following window. Here, we will be updating the age of the person that we inserted in the previous step:
      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).
    • Before you update, you can choose to hit the Count button to see the number of documents that can be updated (in this case, one). Clicking on Find will show you 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.
    • You can choose an Upsert operation in case the documents for the given Find condition are not found.
    • The radio buttons on the bottom right of the preceding screen shows either the output of the getLastError operation or the result after the update, in which case, a query will be executed to find the document(s) updated.
    • The find query, however, is not foolproof and might return different results than those truly updated as a separate query, the same as in the Find textbox. The update and find operations are not atomic.
  11. We have queried on small collections so far. As the size of the collection increases, queries performing full collection scans are not acceptable and we need to create indexes as follows:
    • To create an index by lastName in ascending order and age in descending order, we will invoke db.personTwo.ensureIndex({'lastName':1, 'age':-1}).
    • Using MongoVUE, there is a way to visually create the same index by right-clicking on the collection name on the left-hand side of the screen and selecting Add Index….
    • In the new pop-up window, enter the name of the index and select the Visual tab as shown. Select the lastName and age fields with ascending and descending values, respectively:
      How to do it…
    • Once these details are filled in, click on Create. This should create the index for us by firing the ensureIndex command.
    • You can choose the index to be Unique and Drop Duplicates (which will be enabled when unique is selected), or even create big, long, and running index creations in the background.
    • Note the Json tab next to the Visual tab. This is the place where you can type the ensureIndex command as you do in the shell in order to create the index.
  12. We will see how to drop an index:
    • Simply expand the tree on the left-hand side (as shown in the screen shot in step 9)
    • On expanding the collection, we will see all the indexes created on it
    • Except for the default index on the _id field, all the other indexes can be dropped
    • Simply right-click on the name and select Drop index to drop or click on Properties to view its properties
  13. After seeing how to do the basic CRUD operations and creating an index, let's look at how to execute the aggregation operations:
    • There are no visual tools as in the index creation for aggregation but simply a text area where we enter our aggregation pipeline
    • In the following sample, we perform aggregation on the postalCodes collection to find the top five states by the number of times they appear in the collection
    • We will have the following aggregation pipeline entered:
      {'$project' : {'state':1, '_id':0}},
      {'$group': {'_id':'$state', 'count':{'$sum':1}}},
      {'$sort':{'count':-1}},
      {'$limit':5}
      How to do it…
    • Once the pipeline is entered, hit the Aggregate button to get the aggregation results
  14. Executing MapReduce is even cooler. The use case that we will be executing is similar to the preceding one, but we will see how to implement a MapReduce operation using MongoVUE:
    • To execute a map reduce job, right-click on the collection name in the left-hand side menu, and click on Map Reduce.
    • This option is right above the Aggregation option that we saw in the previous image. This gives us a pretty neat GUI to enter the Map, Reduce, Finalize and the In & Out, as shown in the following image:
      How to do it…
    • The Map function is simply the following:
      function Map() {
        emit(this.state, 1)
      }
    • The Reduce function is the following:
      function Reduce(key, values) {
        return Array.sum(values)
      }
    • Leave the Finalize method unimplemented, and in the In & Out section, fill in the following details:
      How to do it…
    • Click on Go to start executing the MapReduce job.
    • We will print 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)
    • Check the results against those that we got using aggregation.
    • The format of map reduce was chosen as Reduce. For more options and their behavior, visit http://docs.mongodb.org/manual/reference/command/mapReduce/#mapreduce-out-cmd.
  15. Monitoring the server instances is now possible using MongoVUE:
    • To monitor an instance, click on Tools | Monitoring in the top menu.
    • By default, no server will be added, and we will have to click on + Add Server to add a server instance.
    • Select the Local Instance added or any server that you want to monitor, and click on Connect.
    • We will see quite a lot of monitoring details. MongoVUE uses the db.serverStatus command to serve these stats and limit the frequency at which we execute this command on busy server instances, we can choose the Refresh Interval at the top of the screen, as shown in the following image:
    How to do it…

How it works…

What we covered in the previous sections was pretty straightforward for us to perform the majority of our activities as a developer and administrator.

There's more…

Refer to Chapter 4, Administration and Chapter 6, Monitoring and Backups, for recipes on the administration and monitoring of the MongoDB instances.

See also

Note

While writing this book, MongoDB was planning to release a similar data visualisation and manipulation product called Compass. You should check it out https://www.mongodb.com/products/compass.

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

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