Tuesday, March 14, 2017

Basics of MongoDB

MongoDB is a NoSQL database, which means it is not like a relational database where data is handled using relations. It can be used to handle unstructured data like big data.

In MongoDB, a collection acts more like a table, while a document is more like a record or tuple.

Basic Commands
The following are some of the basic commands used in MongoDB:
  • The use command can be used to create and access a database. For example,
              use companyDB;

The above command will create a database if there is no database existing with the name companyDB. If there is a database already existing with the name companyDB, it can be accessed using this command. If such a database already exists, a new database will not be created from that name.

  • The insert command can be used to insert a document to a collection.
                          db.employee.insert({
                       "name" : "Alice",
                       "dept" : ["Accounting", "Marketing"]
            });

The above command will create a collection named employee, and insert the document with the above data to it, if such a collection is not already existing, but in the case where such a collection already exists(collection named employee in this case), then, the document is inserted into the already existing collection.

Each document is uniquely identified using _id. We can provide _id while inserting the document. If we do not provide it while inserting the document, the MongoDB will assign a value to _id by itself, and that will be the case for the above example; the _id will assigned by the MongoDB since we have not provided it.

If we try to add the same document with the same details again(for example, if we try to add the above one again), the addition will be successful since the MongoDB will provide the new document with a new _id value and add it to the database.

  • The find command can be used to get the details of a document.
                             db.employee.find({"name":"Alice"})

This command returns all the fields in the document with their values. Any field can be used with the find command to retrieve documents with a matching field and value. _id field can also be used with the find command, but for that, we have to know the _id. If there is more than one document with matching values for the search field, then all those documents will be returned.

  • The update command can be used to update data in a document.
     db.employee.update({"name":"Alice"}, {$push:{"dept":"Research"}})

Using the first parameter passed, the MongoDB will search for documents with a matching value for that particular field provided, and using the second parameter, it will update the relevant field with the value to be updated. $push is used to insert a value to an array. In this example, "dept" is an array if we take a look back at the insert command. Therefore, it is used in updating the existing array.


  • The remove command can be used to remove or delete a document.
          db.employee.remove({"name":"Alice"})

This command will remove any number of documents with a matching field and value as the one provided. In this case, any document having the field "name" with its value as "Alice" will be deleted. 





No comments:

Post a Comment