How to use MongoDB

How to use MongoDB

Hey everybody welcome to my blog. Today we will be learning about MongoDB.

First of all, why is MongoDB called NoSQL actual thing is it is not SQL but more specifically not only SQL and that means suppose a product has a name, description, price, and slug. Another product might have different data but only a description is missing. In other databases like PHPMyAdmin description is needed but MongoDB doesn't need it because it stores files as JSON. More specifically BSON is an extension of JSON

Now let us install MongoDB

Link: mongodb.com/try/download/community

After download install it if the compass isn't installed by default then go to the following link and install the MongoDB Compass: mongodb.com/try/download/compass

After installed run the compass and

This section is for windows

  • First, search for env in the bottom left search bar and open it
  • Then click on environment variables
  • After that click path on user variables for {{Computer_Account_Name}}
  • Click edit new and paste the path where MongoDB has been installed and add /bin if not added
  • Finally, click Ok, ok, and ok

Now start the Compass App and click Fill in connection fields individually then click connect

Now let's start with MongoDB

First, we need a database so let's create one by clicking on Create a Database then fill in name and a collection name, and click Create Database. The database will be the project name, and collection will be one section name. After that click on Add data and replace it with the following data Make sure to copy the id because ids should be unique

{
    "_id": {
        "$oid": "62076e3a422cad7741615224"
    },
    "name": "YOUR_NAME",
    "email": "YOUR_EMAIL"
}

Replace $oid with your id. Now we just can't create using Compass because we always can't check which user filled data and add things about the user manually so we will use a command line like cmd or PowerShell or zsh or git bash

So open up your favorite command line tool and type mongo and hit enter. This will take us inside MongoDB in the command line

1. Database Commands

View all databases: show dbs
Create a new or switch databases : use dbName
View current Database : db
Delete Database : db.dropDatabase()

2. Collection Commands

Show Collections : show collections
Create a collection named 'users' : db.createCollection('users')
Drop a collection named 'users' : db.users.drop()

3. Row(Document) Commands Show all Rows in a Collection : db.users.find()
Show all Rows in a Collection (Prettified) : db.users.find().pretty()
Find the first row matching the object : db.users.findOne({name: 'Aayush'})
Insert One Row :

 db.users.insert({
    'name': 'Aayush',
    'email': 'aayush@aayush.aayush',
    'password': '84rgfcyuhjdgfhur5787yebd9gbyrju9i',
 })

Insert many Rows :

 db.users.insertMany([{
    'name': 'Rahul',
    'email': 'rahul@rahul.rahul',
    'password': '7u5tgfrgdyghbdsxugyhrfgc7ugfrdx272yhrg',
    }, 
    {'name': 'kauel',
     'email': 'kahuel@kahuel.com',
     'password': '74uyrdtggkahu',
    },
    {'name': 'Tatin',
    'email': 'tatin@tatin.tatinc',
    'password': 'passwordissecretnotgonnatellanyone'
}])

Search in a MongoDb Database : db.users.find({email:'kahuel@kahuel.com'})
Limit the number of rows in output : db.users.find().limit(2)
Count the number of rows in the output : db.users.find().count()
Update a row :

db.users.update({name: 'Kahuel'},
{'name': 'Kahuel',
 'kahuel': 'kahuel@kahuel.com',
 'password': '74uyrdtggkahu',
}, {upsert: true})

Delete Row : db.users.remove({name: 'Tatin'})

Hope now you know about MongoDB. And that's all for today's hope to see you again in the next blog

Did you find this article valuable?

Support Aayush Biswas by becoming a sponsor. Any amount is appreciated!