MongoDB
MongoDB is an open-source document database and leading NoSQL database. This tutorial give you great understanding on MongoDB concepts.
You need to download and install MongoDB from it’s official site. MongoDB
Basic commands on mongo shell
show all available database
Select a particular database to access, eg Student. This will create Employee if it does not already exists.
Show collection in database
To check the currently selected database use the command db
Just type show dbs in your mongo shell and you will see available database
now type
and you will se it switched to database Employee
Just create user with username, password and roles with following lines of code
Now, we need to create collection. Collection may store a number of documents. A collection is analogous to a table of an RDBMS.
Now, let’s insert data into collection using MongoDB’s query
Inorder to see our data we need to write find() query
this output as:
let’s pretify our document using preety() method. This is the method that pretty-prints resulting JSON.
We can use save instead of insert method. like
The difference with save is that if the passed document contains an _id field, if a document already exists with that _id it will be updated instead of being added as new.
Here, insertOne is used to insert only one record and insertMany to insert multiple records.
and you can see the output as:
Update
Now we can update the entire object
we can replace only first matching document. updateOne in new in MongoDB 3.2
we can update the field name by: We are updating Giri to lastname so before updating
Now, after updating (writing above query) we get
If we want to change only the firstname field then we can use $set to specify which field we want to update.
Delete
Deletes all documents matching the query parameters:
New in MongoDB 3.2
If we have multiple Employee’s with same and if we wish to remove only one name then we can write:
Comparison between MongoDB and SQL
The following table shows the SQL and MongoDB terminology
All documents from Employee collection
This is same as the
will return document from our collection whose first_name is Thyame
this is same as the
AND queries
This is same as the
OR queries
This is same as the
PROJECTION
In MongoDB projection means selection only necessary data rather than selecting whole data of document.
The basic syntax of find() method with projection is as follows
In our case
This return all the documents without the age field
If you want to show only the age field then.
1 is used to show the fields whereas 0 is used to hide the fields
Upsert
It is a boolean type. It’s default value is false. If it is set to true then it creates a new document when no document matches the query criteria
Tips about _id 😎
Increment and Decrement
we can Increment by using $inc
wheres as decrement is somehow tricky inorder to decrease we need to add negative value like $inc: -2 it will decrease by 2
Aggregation
Aggregation operations process data records and return computed results.
create some Dummy data:
now inorder to get prettify collection we need to write
and we get
Match is used to match the documents (like SQL where clause)
Project used to populate specific field’s value
it will output as:
project will include _id field automatically unless you secify to disable it:
Sorting
Sorting by name in alphabatical order:
If we want to sort name in reverse order we just need to write -1