MongoDB- INSERT, SELECT, UPDATE, and DELETE Query


This page will help you to perform the basic query/operation with the MongoDB database like SELECT, INSERT, UPDATE, and DELETE. MongoDB is a document database, which means it stores data in JSON-like documents commonly known as a NoSQL database.

Related Post: How to set Username, Password, and Roles to MongoDB Database

1. Create Database

MongoDB created the database and collection automatically when you insert the first record. Use the below query to create or switch to a new database.

> use my_company
switched to db my_company

Remember: use command will not create a database until you insert a record into the collection.

1.1 List all database

> show dbs
admin                    0.000GB
config                   0.000GB
local                    0.000GB
springboot               0.000GB
springboot_mongodb_crud  0.000GB

2. Insert Record

db.collection.insert() or db.collection.save() method is used to insert a new record into the collection. Here collection is replaced with your collection name. It can be used to insert a single as well as multiple records.

Note: In MongoDB, collection means a table in SQL and db.collection.save() method is deprecated since MongoDB 4.2 version.

2.1. Insert Single Record

> db.employee.insert({name:"Atul Rai", email:"[email protected]", age:28, salary:5000.54})

2.2. Insert Multiple Records

> db.employee.insert(
	[
		{name:"Sandeep Sharma", email:"[email protected]", age:28, salary:5333.94},
		{name:"Manish Fartiyal", email:"[email protected]", age:26, salary:5555.4},
		{name:"Santosh Kumar", email:"[email protected]", age:30, salary:7000.74},
		{name:"Dhirendra Chauhan", email:"[email protected]", age:29, salary:4848.44}
	]
)

3. Select Record/s

db.collection.find() method is used to fetch the data from the database. db.collection.find() method with no parameter will fetch all data from the collection similarly db.collection.find(query, projection) method with the parameter will fetch the data conditionally.

3.1 Fetch all

> db.employee.find()
{ "_id" : ObjectId("5fa2c0e19577bba42e1db54a"), "name" : "Atul Rai", "email" : "[email protected]", "age" : 28, "salary" : 5000.54 }
{ "_id" : ObjectId("5fa2c2409577bba42e1db54b"), "name" : "Sandeep Sharma", "email" : "[email protected]", "age" : 28, "salary" : 5333.94 }
{ "_id" : ObjectId("5fa2c2409577bba42e1db54c"), "name" : "Manish Fartiyal", "email" : "[email protected]", "age" : 26, "salary" : 5555.4 }
{ "_id" : ObjectId("5fa2c2409577bba42e1db54d"), "name" : "Santosh Kumar", "email" : "[email protected]", "age" : 30, "salary" : 7000.74 }
{ "_id" : ObjectId("5fa2c2409577bba42e1db54e"), "name" : "Dhirendra Chauhan", "email" : "[email protected]", "age" : 29, "salary" : 4848.44 }

3.2 Fetch with condition

>  db.employee.find({email:"[email protected]"})
{ "_id" : ObjectId("5fa2c0e19577bba42e1db54a"), "name" : "Atul Rai", "email" : "[email protected]", "age" : 28, "salary" : 5000.54 }

3.2 Fetch with multiple condition

> db.employee.find({age:28, salary: { $gt: 5000 }})
{ "_id" : ObjectId("5fa2c0e19577bba42e1db54a"), "name" : "Atul Rai", "email" : "[email protected]", "age" : 28, "salary" : 5000.54 }
{ "_id" : ObjectId("5fa2c2409577bba42e1db54b"), "name" : "Sandeep Sharma", "email" : "[email protected]", "age" : 28, "salary" : 5333.94 }

The following query uses $gt to return documents where the value of salary is greater than 5000.

4. Update Record

db.collection.update({criteria},{$set: {new value}}) method is used to update the existing document of the collection.

> db.employee.update({email:"[email protected]"},{$set:{salary: 8000.99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

5. Delete Record

To delete a record from the collection uses db.tablename.remove({criteria}) method.

5.1 Delete with condition

> db.employee.remove({email:"[email protected]"})
WriteResult({ "nRemoved" : 1 })

5.2 Delete all records

> db.employee.remove({})
WriteResult({ "nRemoved" : 4 })

5.3 Delete/Drop Collection

> db.employee.drop()

References

  1. How to set Username, Password, and Roles to MongoDB Database
  2. Collection Methods – MongoDB Manual

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.