CSCI 305: Introduction to Database Systems

Activities on Mongodb I

You are to complete the following exercises on MongoDB, one of the popular NoSQL database. You will do a few activities on the subject. In this exercise, you will learn the basics of how to log into our local MongoDB database and create your own dataset through the use of bulk loading.

  1. Similar to many database systems, MongoDB allows the user to bulk load datasets, as well as insert individual data elements. MongoDB can load information from a JSON file. We will discuss JSON and MongoDB in class. For now, follow this Python example to load some data into the database so you can get some sense how the system works.
  2. Read the Python program to understand the flow of work. The program loads a small JSON data file and insert its entries to the MongoDB database. The program asks for your credentials. Since the program uses a plain input method, whatever you type will be echoed on the screen, it may be a good idea to redirect the input from a file to avoid typing your user name and password directly as follows
    python pymongo-load-db.py < your-file
    where your-file stores your user name and password.
  3. Log into the MongoDB using the command line interface. At the Linux command line, type the following command
    mongo --host eg-mongodb.bucknell.edu -u username -p --authenticationDatabase databbase database The above command says log into MongoDB using the username and the database name. Consulting with the instructor for the database name and the user name. [1]
  4. After logging into the system, you are able to use the following command commands.
    • use [db-name]
      switch to a particular database, e.g.,
      use xmeng
    • show collections
      show the collections in the database, e.g.,
      show collections
         mybooks
         xmeng
         test
         post
         Address
      
      which lists the collections contained in the database xmeng.
    • db.[coll-name].find()
      list all documents in the collection, e.g.,
      db.post.find()
    • db.[coll-name].find({conditions})
      find and list documents with given conditions, e.g.,
      db.post.find({"likes":{$gt:45}})
      db.post.find({"title":"fifth world"})
      The first command lists the documents that have more than 45 "likes." The second command finds the documents whose title contains the phrase "fifth world." Note: texts are case-sensitive when making comparisons, "text" ≠ "Text"
    • db.[coll-name].drop()
      delete the collection named 'coll-name', e.g.,
      db.Address.drop()
      deletes the collection Address in the database xmeng.
    • db.[coll-name].find().limit(NUMBER) return only NUMBER of documents from the query e.g.,
      db.Address.find({"borough":"Manhattan"}).limit(3)
      will return only the first three documents that meet the condition (the attribute borough has the value of Manhattan.
    • db.[coll-name].find().limit(NUMBER1).skip(NUMBER2)
      skip the first NUMBER2 documents among the ones that meet the condition, then limit the documents to be returned to NUMBER1 e.g., assume executing the find() command returns 10 documents, the following command will skip the first two, then return the next three (i.e., documents 3, 4, and 5)
      db.Address.find({"borough":"Manhattan"}).limit(3).skip(2)
    • quit()
      leaves the MongoDB
  5. If you have some time, please explore the MongoDB using information on the internet, for example the MongoDB tutorial by Tutorial Point [2].

References

  1. AskLIT. Accessed 2018-04-18.
  2. MongoDB tutorial by TutorialPoint. Accessed 2018-04-18.
  3. Find more references from this link.