Insert Document into MongoDB

Author: Al-mamun Sarkar Date: 2020-09-11 10:35:08

In this lesson, I will show you how to insert a document on MongoDB collections. I will show you how to insert one document and how to insert multiple documents.

 

Insert One Document to collection:

db.users.insert({
	'name': "Mamun Sarkar",
	'email': "mamun@gmail.com",
	'phone': 1242343,
	'address': "Mirpur DOHS, Dhaka",
})

OR

db.users.insertOne({
	'name': "Jone doe",
	'email': "jone@gmail.com",
	'phone': 1242343,
	'address': "Mirpur DOHS, Dhaka",
})

 

Insert Multiple Document:

db.users.insertMany([
{
	'name': "Jastib Bos",
	'email': "bos@gmail.com",
	'phone': 1242343,
	'address': "Mirpur DOHS, Dhaka"
},
{
	'name': "Jone Sina",
	'email': "sina@gmail.com",
	'phone': 1242343,
	'address': "Mirpur DOHS, Dhaka"
}
])

Useing insert:

db.users.insert([
{
	'name': "Jastib Bos",
	'email': "bos@gmail.com",
	'phone': 1242343,
	'address': "Mirpur DOHS, Dhaka"
},
{
	'name': "Jone Sina",
	'email': "sina@gmail.com",
	'phone': 1242343,
	'address': "Mirpur DOHS, Dhaka"
}
])

 

Insert Many Data to Employees table:

db.employees.insertMany([
{
	'name': "Jastib Bos",
	'email': "bos@gmail.com",
	'phone': 1242343,
	'salary': 500
},
{
	'name': "Jone Sina",
	'email': "sina@gmail.com",
	'phone': 1242343,
	'salary': 600
},
{
	'name': "Another Name",
	'email': "another@gmail.com",
	'phone': 1242343,
	'salary': 200
},
{
	'name': "Nice Guy",
	'email': "nice@gmail.com",
	'phone': 1242343,
	'salary': 1000
},
{
	'name': "Jone Jos",
	'email': "jos@gmail.com",
	'phone': 1242343,
	'salary': 800
},
{
	'name': "Jone Sarma",
	'email': "sharma@gmail.com",
	'phone': 1242343,
	'salary': 150
}
])