WHAT IS MONGODB ?
MongoDB is an open-source document database NoSQL database. The main features of MongoDB are support ad hoc queries(In MongoDB, you can search by field, range query), indexing, duplication of data(MongoDB can run over multiple servers.),supports JSON dats model with dynamic schemas, provides high performance, etc. MongoDB stores data as documents.
MongoDB CRUD Operations
Create operations
Let’s perform the crud operations using the command prompt.
$ mongod
The above command shows you the connections on port 27017. Now our This terminal is connected to the MongoDB database.
Now open a new terminal type only mongo and press enter.
$ mongo
>
Now you can see the ‘>’ symbol in the terminal. It means you are ready to use the mongo shell. Mongo shell facilitates to interact with our MongoDB databases on our local system using the command line.
> help
If we enter the help command,
When we execute the show dbs command, we can see, it comes preloaded with three databases: admin, config and local.
Let’s create a database.
db command shows the current database we are in.
MongoDB provides the following methods to insert documents into a collection:
When we want to insert only one data set
>db.collection.insertOne()
When we want to insert many data sets
>db.collection.insertMany()
Let’s do an example.
db.influencers means the collection that we want to make is the one that’s called influencers. By using insertOne we get to insert a Javascript object.
By using the show collections command, we can see our collections.
Read Operations
Read operations retrieve documents from a collection.
> db.collection.find()
By using the above command, we can read operations.
Let’s see an example.
if you wanted to make a specific query for a piece of data in your collection then you can add a query in there.
Here we select the influencers who are in the beauty category.
So in our example we could say db.influencers.find and we’re going to find all the records that whose category is beauty. Then in our second parameter we’re going to put in our projection, so the field that we want back And we want the name.
Update operations
> db.collection.updateOne({})
> db.collection.updateMany({})
> db.collection.replaceOne({})
By using the above commands, we can update our collection. Inside the parenthesis, we can write queries.
$set allows us to set a new field and value into our document.
Delete Operations
> db.collection.deleteOne({})> db.collection.deleteMany({})
By using the above commands, we can delete our collection. Inside the parenthesis, we can write queries.
As an example by using the above command we can delete the record with id no 2.
Let’s think the influencer document has another document called reviews. One influencer can have many reviews. Below code shows how we have implemented that relationship.
db.influencers.insertOne(
{
_id: 3,
name:"yeli",
category:"beauty",
reviews:
{
reviwerName: "Bunny",
rating: 4,
review:"easier to work with her"
},{
reviwerName: "Hunny",
rating: 4,
review:"dedicated person"
}}
)
Conclusion
In this article we discussed about CRUD operations of MongoDB and how we can build relationships among the documents.
Thank you for taking your valuable time to read my article. Stay tuned!!