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 })

MongoDB Query

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

 Show dbs

MongoDB Query

Drop database:  Below command is used to drop database

db.dropDatabase()

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

MongoDB Query

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
  • }
MongoDB Query

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

db.employee.find()

result:

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

MongoDB Query

 Pretty : to format data into readable format use below query

db.employee.find().pretty()

result:

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

Insert : to insert into collection

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

MongoDB Query

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”)
  •         ]
  • }
MongoDB Query

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 })

MongoDB Query

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

MongoDB Query

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 })

MongoDB Query

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 })

MongoDB Query

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.

MongoDB Query

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 })

MongoDB Query

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

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

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

MongoDB Query

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

MongoDB Query

Drop the collection

Db.collectionName.drop()

You may also like...

Leave a Reply

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