Learn MongoDB Basics in 20 Minutes

Learn MongoDB Basics in 20 Minutes

MongoDB is a very popular NoSQL database and it is a document-oriented. It has collections and documents. It supports JSON formatted documents. This article teaches you how to set up MongoDB in your computer and how to do CRUD operations using the mongo terminal. Reading this Mongodb tutorial you can learn the basics of MongoDB within 20 minutes. Please follow the steps are given below to understand this article.

  1. Firstly you have to download MongoDB tar.gz and extract it.
  2. The next step is to go to the bin folder
  3. After that create a folder to store the Mongo DB data and point it to mongo DB
  4. Then open the terminal in the bin folder
  5. Make them as executable
    chmod +x mongod
  6. Point data folder which we created recently called “Mongodata”
    ./mongod --dbpath ~/Mongodata/
  7. Open a new terminal in the same location and start mongo
    ./mongo
  8. Then you can see available databases executing this on the command line
    show databases;
  9. Then create a database called “testMongo”
    use testMongo;
  10. After that create a collection called “users” inside testMongo.
    db.createCollection(“users”);
  11. Then you can insert a few documents inside users’ collections as given below.
    db.users.insert({“name” : “Bhanuka”, “age”: “27”, “gender”: “male”, “country” : “Sri Lanka”});
    db.users.insert({“name” : “Prabath”, “age”: “28”, “gender”: “male”, “country” : “Sri Lanka”});
    db.users.insert({“name” : “Sajith”, “age”: “25”, “gender”: “male”, “country” : “USA”});
    db.users.insert({“name” : “Sarah”, “age”: “25”, “gender”: “female”, “country” : “German”});
  12. The next step is to search the data
    db.users.find({});
  13. Then you can update the data using the below query
    db.users.update({"name" : "Prabath"},{$set: {"country": "Italy"}});
  14. Next, try to delete the data
    db.users.remove({"name" : "Bhanuka"});

In my next article, I will teach you to learn the basics of MongoDB with Java application and how to perform CRUD operations from inside it.