mongosdb crud operations and Query Documents

 Mongodb ,inserting, updating,finding,deleting elements

Note : This blog in progress 

With find one does not required pretty funtions 


Useful Resources & Links
Useful Articles/ Docs:
Resetting Your Database

Important: We will regularly start with a clean database server (i.e. all data was purged) in this course.

To get rid of your data, you can simply load the database you want to get rid of (use databaseName) and then execute db.dropDatabase().

Similarly, you could get rid of a single collection in a database via db.myCollection.drop().






MongoDB Installation FAQ + Support

IMPORTANT - For macOS Catalina Users

If you're on macOS Catalina, there has been a hidden change in the operating system. You're NOT able to use /data/db as a database folder anymore. Instead, you have to use a non-root path (e.g. ~/data/db).

Everything should go smooth when installing MongoDB but in case you're facing any issues, here are a couple of helpful hints + resources that should help you.

1) Check the Official Docs

It sounds cheap but the official docs are really good. If you're facing any issues or you're not sure about some step, check the official installation guide for your operating system and make sure you follow the instructions step-by-step.

Windows => https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/

macOS => https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

Linux => https://docs.mongodb.com/manual/administration/install-on-linux/

2) The Database Path

MongoDB needs to store your database data somewhere - and it does so in real files. On Windows, these files are stored in a path which you define during installation (for the Windows service).

On macOS and Linux, MongoDB by default uses /data/db as the storage path. It also assumes a similar path (C:\data\db) on Windows when NOT using the MongoDB service (i.e. when manually executing the mongod command).

You can always change the path by running:

mongod --dbpath "my/new/path" (on Windows, use backslashes + the drive name => mongod --dbpath "C:\my\path").

3) Permissions

Make sure that MongoDB is allowed to write to the path you specified. If you're getting any permission errors, try running

sudo mongod

on macOS and Linux.

On Windows, run the command prompt as administrator (right click on the command prompt executable => "Run as administrator").


CRUD operations & MongoDB





InsertOne Method 


db.unserInfo.insertOne({ "fistName": "Raj", "lastName": "Yadav", "Age":30, "Adress" :"Pune", "maritalstatus": true, "NRI": true, "officeAddress": "pune", "Mob":919822222233, "_id": "rajidzxy" , "emp": {     "jobtitle": "software engineer",     "companyname": "hcl",     "Sal": "12 Lac",    "exp":10   }, })




insert with custom id ( "_id": "rajidzxy"  )


db.unserInfo.insertOne({ "fistName": "Raj", "lastName": "Yadav", "Age":30, "Adress" :"Pune", "maritalstatus": true, "NRI": true, "officeAddress": "pune", "Mob":919822222233, "_id": "rajidzxy"  "emp": {     "jobtitle": "software engineer",     "companyname": "hcl",     "Sal": "12 Lac",    "exp":10   }, })



Find command  


db.unserInfo.find().pretty()

{

"_id" : ObjectId("5f64f0490896f4aa112834f0"),

"fistName" : "Raj",

"lastName" : "Yadav",

"Age" : 30,

"Adress" : "Pune",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919822222233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "12 Lac",

"exp" : 10

}

}

{

"_id" : "rajidzxy",

"fistName" : "Raj",

"lastName" : "Yadav",

"Age" : 30,

"Adress" : "Pune",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919822222233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "12 Lac",

"exp" : 10

}

}



Insermany with array object 


db.unserInfo.insertMany( [{

"fistName": "Raj",

"lastName": "Yadav",

"Age":30,

"Adress" :"Pune",

"maritalstatus": true,

"NRI": true,

"officeAddress": "HYD",

"Mob":919822222233,

  "emp": {

    "jobtitle": "software engineer",

    "companyname": "hcl",

    "Sal": "12 Lac",

   "exp":10

  },

},

{

  "fistName": "Sachin",

"lastName": "Dubey",

"Age":30,

"Adress" :"VNS",

"maritalstatus": true,

"NRI": true,

"officeAddress": "Delhi",

"Mob":919820002233,


"emp": {

    "jobtitle": "software engineer",

    "companyname": "hcl",

    "Sal": "12 Lac",

   "exp":9

  },


}])




custom id with Array object 


db.unserInfo.insertMany([{ "_id": "R109", "fistName": "Raj", "lastName": "Yadav", "Age":30, "Adress" :"Pune", "maritalstatus": true, "NRI": true, "officeAddress": "pune", "Mob":919822222233,   "emp": {     "jobtitle": "software engineer",     "companyname": "hcl",     "Sal": "12 Lac",    "exp":10   }, }, { "_id": "R108",   "fistName": "Sachin", "lastName": "Dubey", "Age":30, "Adress" :"VNS", "maritalstatus": true, "NRI": true, "officeAddress": "pune", "Mob":919820002233,  "emp": {     "jobtitle": "software engineer",     "companyname": "hcl",     "Sal": "12 Lac",    "exp":9   },  }] )



Find command 


 db.unserInfo.find({"_id":"R109"}).pretty()

{

"_id" : "R109",

"fistName" : "Raj",

"lastName" : "Yadav",

"Age" : 30,

"Adress" : "Pune",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919822222233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "12 Lac",

"exp" : 10

}

}






Document within documents 


db.unserInfo.find({"emp.companyname" : "hcl"}).pretty()



Document updateOne  


db.unserInfo.updateOne({_id:"sys101"},{$set :{"qualificatiosn":"MCA"}})

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }




db.unserInfo.find({"qualificatiosn":"MCA"}).pretty()

{

"_id" : "sys101",

"fistName" : "Raj",

"lastName" : "Yadav",

"Age" : 30,

"Adress" : "Pune",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919822222233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "12 Lac",

"exp" : 10

},

"qualificatiosn" : "MCA"

}










ReplaceOne  

Replace 2 fields     "qualificatiosn" : "MCA", "graduation" : "BSC"  


{

"_id" : "sys101",

"fistName" : "Raj",

"lastName" : "Yadav",

"Age" : 30,

"Adress" : "Pune",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919822222233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "12 Lac",

"exp" : 10

},

"qualificatiosn" : "MCA",

"graduation" : "BSC"

}





 db.unserInfo.replaceOne({_id:"sys101"},{ "_id" : "sys101", "fistName" : "Raj", "lastName" : "Yadav", "Age" : 30, "Adress" : "Pune", "maritalstatus" : true, "NRI" : true, "officeAddress" : "pune", "Mob" : 919822222233, "emp" : { "jobtitle" : "software engineer", "companyname" : "hcl", "Sal" : "12 Lac", "exp" : 10 } })


Find and check  replace info 


db.unserInfo.find({_id:"sys101"}).pretty()





Find the limited rows 

single row 


db.unserInfo.find({}, {Adress: 1 ,_id:0}).pretty()

{ "Adress" : "Pune" }

{ "Adress" : "Pune" }

{ "Adress" : "Pune" }

{ "Adress" : "VNS" }

{ "Adress" : "Pune" }

{ "Adress" : "Pune" }

{ "Adress" : "VNS" }

{ "Adress" : "Pune" }

{ "Adress" : "VNS" }

{ "Adress" : "Pune" }

{ "Adress" : "VNS" }

{ "Adress" : "LKO" }

{ "Adress" : "LKO" }

{ "Adress" : "VNS" }

{ "Adress" : "LKO" }

{ "Adress" : "VNS" }



Double  row 



db.unserInfo.find({}, {fistName: 1 ,lastName: 1,_id:0}).pretty()

{ "fistName" : "Raj", "lastName" : "Yadav" }

{ "fistName" : "Raj", "lastName" : "Yadav" }

{ "fistName" : "Raj", "lastName" : "Yadav" }

{ "fistName" : "Sachin", "lastName" : "Dubey" }

{ "fistName" : "Raj", "lastName" : "Yadav" }

{ "fistName" : "Raj", "lastName" : "Yadav" }

{ "fistName" : "Sachin", "lastName" : "Dubey" }

{ "fistName" : "Raj", "lastName" : "Yadav" }

{ "fistName" : "Sachin", "lastName" : "Dubey" }

{ "fistName" : "Tam", "lastName" : "cook" }

{ "fistName" : "Sachin", "lastName" : "Dubey" }

{ "fistName" : "Nitin", "lastName" : "Verma" }

{ "fistName" : "shekhar", "lastName" : "Verma" }

{ "fistName" : "ranjeet", "lastName" : "Verma" }

{ "fistName" : "shekhar", "lastName" : "Verma" }

{ "fistName" : "ranjeet", "lastName" : "Verma" }






Nested and embedded document 


Emp is a  embedded document 


added hobbies with Aarray  


db.unserInfo.updateMany({"Age":{$gt:20}},{$set:{hobies:["sports","cooking"]}})




{

"_id" : ObjectId("5f658aae0896f4aa112834f7"),

"fistName" : "ranjeet",

"lastName" : "Verma",

"Age" : 28,

"Adress" : "VNS",

"maritalstatus" : true,

"NRI" : false,

"officeAddress" : "chen",

"Mob" : 919820002233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "12 Lac",

"exp" : 9

},

"hobies" : [

"sports",

"cooking"

]

}



Find only hobbies 


db.unserInfo.findOne().hobies

[ "sports", "cooking" ]




find All 

 db.unserInfo.find({hobies:"sports"}).pretty()


find only one data 


db.unserInfo.findOne({"emp.exp":9})




db.unserInfo.find({"emp.exp":{$gt:6}}).pretty()




Find with conditions  Age > 30 



db.unserInfo.find({"Age":{$gt:30}}).pretty()

{

"_id" : "sys1010",

"fistName" : "Tam",

"lastName" : "cook",

"Age" : 35,

"Adress" : "Pune",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919822222233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "hcl",

"Sal" : "18 Lac",

"exp" : 10

}

}

{

"_id" : "sys1020",

"fistName" : "Sachin",

"lastName" : "Dubey",

"Age" : 40,

"Adress" : "VNS",

"maritalstatus" : true,

"NRI" : true,

"officeAddress" : "pune",

"Mob" : 919820002233,

"emp" : {

"jobtitle" : "software engineer",

"companyname" : "cisco",

"Sal" : "15 Lac",

"exp" : 9

}

}

Comments