online learning, blog, anil singh



Feb 15

MongoDB Basics Primer Series - Step 2

Objective: First dive into the MongoDB CRUD operations and know third-party tools to connect to MongoDB.


Prerequisite: MongoDB Basics Primer Series - Step 1


I hope your previous run through of Step 1 of this series was smooth. Now that you have set up the environment yourself and done some tinkering, you must be brimming with confidence. It is time to put forward some basic concepts on MongoDB.

MongoDB is a NoSQL database as it is based on the following NoSQL pillars,

  1. Non-relational data model. It is a JSON (document) store. You must have gone through the JSON in Step 1, right?
  2. Cluster friendly, highly scalable.
  3. Schemaless. The structure of the documents is not required to be pre-declared and need not be consistent across multiple documents in the same collection. Remember the RDBMS world!!! all rows in a table have the same fields whether the field for an instance is required or not, thus giving rise to sparse data, generic column names and wide tables.


Load Data into MongoDB

To start firing queries at the server, we need some data. So let's prepare to load some from a JSON file,

  1. Open a Terminal window and create a Temp directory to hold upload data file - $ mkdir ~/MongoDbWorkSpace/Temp
  2. Add MongoDB utilities and programs to the PATH variable so that you need not go every time to the bin folder to run them - $ echo export PATH=$PATH:~/MongoDB_3.2.1/bin >> .bashrc - '>>' appends to '.bashrc' file.
  3. Close the Terminal window and open a new one or just run the following in the current Terminal - $ source ~/.bashrc - This refreshes the bash env. to include the new settings.
  4. Check whether you have access to MongoDB utilities and programs from anywhere in the shell - $ which mongo - Did it point to the right path?
  5. Start the MongoDB server aka the mongod process, refer to  Step 1 of the series, in case you are still struggling.
  6. The real thing students.json (right click and save link as in the newly created Temp folder as above). Now import the data from this JSON file into the database - $ mongoimport -d MyDB -c Students ~/MongoDbWorkSpace/Temp/students.json - Did you realize!! you just did a successful import of 200 documents in MongoDB, WOW, that's significant step in your journey. The '-d' option specifies the database into which the data must be loaded. In case the database doesn't exist, like in this case, it will be created. The data was loaded into the collection 'Students' specified against '-c' option.


A Short Talk on the Uploaded Data

It is essential to understand what we have just loaded. The data contains scores of students in homework, quiz and exam. There are 3 fields, '_id', 'name' and 'scores'. It is important to understand here that the field 'scores' contains array of documents, these documents have 2 fields 'type' and 'score'. Getting interesting!!

Open a Terminal window and connect to MongoDB - $ mongo MyDB - MyDB is the database into which you uploaded the data a moment ago.

Now read a document - > db.Students.findOne() - here is what you get



You traveled well this far, now let us get into the thick of the jungle...ready.....steady GO


Firing the Queries

  1. > db.Students.findOne() - Fetches only one document from the entire lot.
  2. > db.Students.find() - Displays 20 documents at a time, for the rest, type 'it' (iterate) on the shell.
  3. db.Students.find().pretty() - The method 'pretty()' formats the output and makes it more readable.
  4. db.Students.find().limit(5) - Limits the output only to 5 documents.
  5. db.Students.find().skip(10).limit(5) - Skips first 10 documents and then limits the output only to 5 documents.
  6. > db.Students.find({}, {name:1}) - Projects fields 'name' and '_id'.
  7. db.Students.find({}, {name:1, _id:0}) - This one suppresses field '_id' as well. Unlike other fields, '_id' needs to be explicitly suppressed.
  8. db.Students.find({name:"Bao Ziglar"}).pretty() - Picks up documents where  name is "Bao Ziglar". Did you see, there are two students with the name?
  9. db.Students.find({name:"Bao Ziglar"}, {name:1}) - Picks up documents where  name is "Bao Ziglar" and projects fields 'name' and 'id'.
  10. > db.Students.find({scores:{$elemMatch:{type:"exam", score:{$gt:97}}}}) - Picks the documents with documents in array 'scores' having 'type' as 'exam' and 'score' above 97. There are 4 such documents.
  11. > db.Students.find({name:"Bao Ziglar"}, {name:1}).count() - Counts the number of matching documents.
  12. db.Students.find({},{name:1}).sort({name:1}) - Sorts the output by 'name' in ascending order.
  13. db.Students.find({},{name:1}).sort({name:-1})  - Sorts the output by 'name' but in descending order. Look carefully at the sorted data, there is an interesting observation in the first two lines.
  14. > db.Students.insert({ "_id" : 200, "name" : "Bob Light", "scores" : [ { "type" : "exam", "score" : 88.11742562118049 }, { "type" : "quiz", "score" : 50.61295450928224 }, { "type" : "homework", "score" : 30.86823689842918 }, { "type" : "homework", "score" : 6.861613903793295 } ] }) - Inserts a new document in the collection. So now you have 201 documents in the collection, isn't it!! Run a count on the collection and check. Try inserting the same document again. What do you get? Duplicate key error!!! right, because in a collection all '_id' must have unique values.
  15. > db.Students.update({_id:200}, {$set:{name:"Bob Tyler"}}) - Updates the 'name' for document with '_id'  200.
  16. > db.Students.update({_id:200}, {$set:{grade:"A"}}) - Did you notice? The field 'grade' did not exist, so it was added in the document. Of all the 201 documents, this is the only document with field 'grade' now. Ain't this different from what you observe in a RDBMS? Remember, a Document corresponds to a Row and a Collection corresponds to a Table.
  17. >{_id:200, name:"Bob Tyler"}) - Since '_id' 200 exists, the save method updated, rather replaced the document. Now you have only two fields for the document. save() replaces if a match is found else inserts.
  18. > db.Students.remove({_id:200}) - Deletes the document with '_id' 200. Try to run a find on the document, did you get it? How many documents do you have in the collection now? (clue: run a count on the collection).
  19. > db.Students.remove({}) - Deletes all the documents in the collection 'Students', but the collection still exists. Just check, does it? (clue: run 'show collections')
  20. > db.Students.drop() - Drops the collection from the database. Check whether the collection exists now.
  21. db.dropDatabase() - Drops the database. So now, you do not have your database 'MyDB' anymore. Run mongoimport , as above, to reinstate the working set and run the queries.

Not only read through each of the statements above but also run them on your Mongo shell, it will help you get familiar with various CRUD operations in MongoDB.


Third-party tools to connect to MongoDB

MongoChef is one such UI based tool that helps you to view the databases and run queries. You can use it for free against non-commercial license though with limited features.


The interface is neat, responsive and sophisticated. You can save your queries as scripts  which you can recall later. In case you find it difficult to read the resultsets in Mongo shell, I recommend you to use this one.

Robomongo is not far behind. You can use this tool as well in your initial learning phase.


Although the above mentioned tools provide an effective alternative to Mongo shell, but do remember, they are not a replacement for Mongo shell, :).


I hope this Step 2 of the MongoDB Basics Primer Series helps you get further acquainted with the database. Read more about the queries and run them on your environment. In case you are switching over from the RDBMS world, reflect back on the various queries you had framed and figure out yourself how can they be represented and run on MongoDB. Do always remember, it would be unwise to pit a RDBMS against a NoSQL datastore as they cater to different use cases.

In the upcoming Step 3 of the series, I will cover more on the CRUD operations and touch upon Aggregation Framework.Till then, fire queries on your MongoDB server.




Recommended for further reading,

  1. Read more on MongoDB CRUD here.
  2. Read more on mongoimport here.



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>