MONGO DB -is a NO SQL Database(It is not Relational DB )

  • It is based on BSON ( Binary Script Object Notation )

Mongo VS Relational Table

  1. Collections / Tables
  2. Documents / rows
  3. Field / Columns
  4. The collection isn’t strict about what goes in it / Tables is very strict

MongoDB Working:

→ It can be used with js, java, python, and more…

→ Working with MongoDB directly is a little bit tricky so we use some additional libraries like mongoose for js which is more popular and easy to access the MongoDB.

→ Every data we insert must be in form of JSON which is converted as BSON by MongoDB, for Instance

{"hello": "world"} →\x16\x00\x00\x00           // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value
\x00 // 0x00 = type EOO ('end of object')

→ This is because BSON is faster to access as it is a binary form and also it also generates the Object Id for all fields in the DB, ( Object Id is nothing but the 12bit primary key which is automatically created by MongoDB )

NOTE: primary key can also be created.

Mongoose is a MongoDB ODM (Object Document Mapper) for Node.

To work with MongoDB at first we can start with mongo shell

C: -> Program Files -> MongoDB -> Server -> 4.0(version) -> bin

then open the command prompt in the current location and execute the command

→ mongo ( or ) mongo.exe

→ show dbs command is used to list the databases

→ use databaseName is used to create a database, for Instance

> show dbs> use studentDB 

CRUD in MongoDB

  1. Create
  2. Read
  3. Update
  4. delete

1.CREATE:

→ insertOne(data,options)

> db.add.insertOne({“name”:”John”,”age”:20,email:”john@gmail.com”})

→ insertMany(data,options)

> db.add.insertMany([
{"name":"Ben","age":33,email:"Ben@gmail.com"} ,
{"name":"Doe",age:25,email:"Doe@gmail.com"} ,
])

2.Read:

→ find()

> db.collectionName.find()

Result:

{ "_id" : ObjectId("5f2949ddb73db889f7d39e42"), "name" : "John", "age" : 20, "email" : "john@gmail.com" }
{ "_id" : ObjectId("5f294ac3b73db889f7d39e43"), "name" : "Ben", "age" : 33, "email" : "Ben@gmail.com" }
{ "_id" : ObjectId("5f294ac3b73db889f7d39e44"), "name" : "Doe", "age" : 25, "email" : "Doe@gmail.com" }

to make it more readable use pretty()

> db.collectionName.find().pretty()

Result:

{
"_id" : ObjectId("5f2949ddb73db889f7d39e42"),
"name" : "John",
"age" : 20,
"email" : "john@gmail.com"
}
{
"_id" : ObjectId("5f294ac3b73db889f7d39e43"),
"name" : "Ben",
"age" : 33,
"email" : "Ben@gmail.com"
}
{
"_id" : ObjectId("5f294ac3b73db889f7d39e44"),
"name" : "Doe",
"age" : 25,
"email" : "Doe@gmail.com"
}

→findOne() ( NOTE : It return the first one which is matched )

> db.collectionName.find({name:"John"}).pretty()

Result:

{
"_id" : ObjectId("5f2949ddb73db889f7d39e42"),
"name" : "John",
"age" : 20,
"email" : "john@gmail.com"
}

3. Update

→ updateOne(filter,option)?

> db.collectionName.updateOne( { name:"John" } , 
{ $set: { email:"john@yahoo.com" } } )
> db.collectionName.find({name:"John"}).pretty()

Result:

{
"_id" : ObjectId("5f2949ddb73db889f7d39e42"),
"name" : "John",
"age" : 20,
"email" : "john@yahoo.com"
}

→updateMany(filter,option)

> db.collectionName.updateMany( {} , { $set: { isVerified:true } } )> db.collectionName.find().pretty()

Result:

{
"_id" : ObjectId("5f2949ddb73db889f7d39e42"),
"name" : "John",
"age" : 20,
"email" : "john@yahoo.com",
"isVerified" : true
}
{
"_id" : ObjectId("5f294ac3b73db889f7d39e43"),
"name" : "Ben",
"age" : 33,
"email" : "Ben@gmail.com",
"isVerified" : true
}
{
"_id" : ObjectId("5f294ac3b73db889f7d39e44"),
"name" : "Doe",
"age" : 25,
"email" : "Doe@gmail.com",
"isVerified" : true
}

4. Delete

→ deleteOne(filter,option)

> db.collectionName.deleteOne( {name:"John"} )> db.collectionName.find().pretty()

Result:

{
"_id" : ObjectId("5f294ac3b73db889f7d39e43"),
"name" : "Ben",
"age" : 33,
"email" : "Ben@gmail.com",
"isVerified" : true
}
{
"_id" : ObjectId("5f294ac3b73db889f7d39e44"),
"name" : "Doe",
"age" : 25,
"email" : "Doe@gmail.com",
"isVerified" : true
}

→ deleteMany(filter,option)

> db.collectionName.deleteMany( {isVerified:true} )> db.collectionName.find().pretty()

Result:

Nothing will be displayed all the documents are deleted