MongoDB Query

Create Database :

Use databasename : this command is used to create database

Create collection: below command is used to create the collection

db.createCollection(“departments”)

result:      { “ok” : 1 }

show collections

result: departments  tempdb.employees 

Insert into Collection

db.emp.salary.insert({“first_name”:”raj”,”salary”:10000});

result: WriteResult({ “nInserted” : 1 })

Sagar-Jaybhay-Create-Collection

List all database: Below command is used to show all database.

 Show dbs

Sagar-Jaybhay-Show-DBs

Drop database:  Below command is used to drop database

db.dropDatabase()

result : { “dropped” : “myDropDb”, “ok” : 1 }

Sagar-Jaybhay-Drop-database

Database Stat:-

db.stats()

result:

  • {
  •         “db” : “tempdb”,
  •         “collections” : 1,
  •         “views” : 0,
  •         “objects” : 1,
  •         “avgObjSize” : 67,
  •         “dataSize” : 67,
  •         “storageSize” : 16384,
  •         “numExtents” : 0,
  •         “indexes” : 1,
  •         “indexSize” : 16384,
  •         “fsUsedSize” : 123889491968,
  •         “fsTotalSize” : 150151688192,
  •         “ok” : 1
  • }
Sagar_jaybhay-Db-Stats

Select Query: use below query to select all data from collection

db.employee.find()

result:

{ “_id” : ObjectId(“5c8a21d029dfc7b1d58a149f”), “first_name” : “sagar”, “last_name” : “jaybhay” }

Sagar_jaybhay-Get-Collection

 Pretty : to format data into readable format use below query

db.employee.find().pretty()

result:

  • {
  •         “_id” : ObjectId(“5c8a21d029dfc7b1d58a149f”),
  •         “first_name” : “sagar”,
  •         “last_name” : “jaybhay”
  • }
sagar-Jaybhay-Pretty-Command

Insert : to insert into collection

db.employee.insert({“first_name”:”vinya”,”last_name”:”shingote”,”salary”:40000,”department”:”testing”});

To insert many document in single you can use insertMany to insert multiple document

db.department.insertMany([{“departmentId”:2,”departmentName”:”HR”},{“departmentId”:3,”departmentName”:”Finance”},{“departmentId”:4,”departmentName”:”Developer”}])

result:

  • {
  •         “acknowledged” : true,
  •         “insertedIds” : [
  •                 ObjectId(“5c8b2bde0f16f01efd8d7c5d”),
  •                 ObjectId(“5c8b2bde0f16f01efd8d7c5e”),
  •                 ObjectId(“5c8b2bde0f16f01efd8d7c5f”)
  •         ]
  • }
Sagar-jaybhay-Insert-many-records

Update: below command is used to update the document first brace is used to write selector or a) condition to validate then in second braces $set operator to used update value.

db.employee.update({“first_name”:”sagar”,”last_name”:”jaybhay”},{ $set: {“first_name”:”sagoba”} })

result: WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })

Sagar-Jaybhay-Update-Collection

Also by using update command we able to insert new field in existing document. Below is command

Update all document: below command is used to update all document at once but we are not set multiparameter flag so it update only first document. To update all document we pass first brace is empty.

db.employee.update(
… {},
… { $set: {“company”:”Personal INC”} }
… )

result: WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })

To update all document in collection we required to set multi flag to true after the set parameter.

Update with Unset operator by using this $unset operator you can remove the field

db.employee.update({},{ $unset:{“salary”:10} },{multi:true})

result: WriteResult({ “nMatched” : 3, “nUpserted” : 0, “nModified” : 3 })

Update with Upsert: upsert flag is used with update. The query run on collection then if upsert flag is set true, if no conditioned is matched update will insert a record.

Update with inc Operator: – this command is used to increment the number field by specifying number. In this if you use negative number then value will decrease.

db.employee.update(
… {},
… {$inc:{“salary”:1000}},
… {multi:true})

result: WriteResult({ “nMatched” : 4, “nUpserted” : 0, “nModified” : 4 })

Remove Document or Collection : in remove function first parameter is removing criteria

db.department.remove({“departmentId”:1})

result: db.department.remove({“departmentId”:1})

Remove with justOne parameter: – by using justOne parameter in remove function will remove only one record even if multiple document present in collection

Drop the collection

Db.collectionName.drop()

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *