Home >>MongoDB Tutorial >MongoDB Insert Document

MongoDB Insert Document

How to Insert Document(Data) using MongoDB

In this chapter, we'll learn how to insert the MongoDB collection document.

The insert() Method

You need to use MongoDB's insert() or save() method to insert data into MongoDB's collection.

Syntax

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insert(document)

Example

>db.users.insert({
... _id : ObjectId("507f191e810c19729de860ea"),
... title: "MongoDB Overview",
... description: "MongoDB is no sql database",
... by: "phptpoint",
... url: "http://www.phptpoint.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
>

Mycol is our name for the collection here, as created in the previous chapter. If the collection does not exist in the database, this collection will be created by MongoDB and a document will be insert into it.

If the _Id parameter is not specified in the inserted document, then MongoDB assigns this document to a specific ObjectId.

_Id is a hexadecimal number of 12 bytes unique to each document in a collection. 12 bytes are divided as follows—

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)

An array of documents may also be passed to the insert() method as shown below:

>db.createCollection("post")
>db.post.insert([
	{
		title: "MongoDB Overview",
		description: "MongoDB is no SQL database",
		by: "phptpoint",
		url: "http://www.phptpoint.com",
		tags: ["mongodb", "database", "NoSQL"],
		likes: 100
	},
	{
	title: "NoSQL Database",
	description: "NoSQL database doesn't have tables",
	by: " phptpoint",
	url: "http://www. phptpoint.com",
	tags: ["mongodb", "database", "NoSQL"],
	likes: 20,
	comments: [
		{
			user:"user1",
			message: "My first comment",
			dateCreated: new Date(2013,11,10,2,35),
			like: 0
		}
	]
}
])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
>

To insert a document, db.post.save(document) may also be used. If you do not specify an Id in the document, the save) (method will function the same as the insert() method. If you specify I d, then the whole document data containing _Id as specified in the save() method will be replaced.

The insertOne() method

You can use this method if you need to insert just one document into the collection.

Syntax

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insertOne(document)

Example

The following example creates a new collection called empDetails and uses the insertOne() method to insert a document.

> db.createCollection("empDetails")
{ "ok" : 1 }
> db.empDetails.insertOne(
	{
		First_Name: "anshu",
		Last_Name: "verma",
		Date_Of_Birth: "1997-07-05",
		e_mail: "[email protected]",
		phone: "9054387510"
	})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
}
>

The insertMany() method

You may use the InsertMany() method to insert multiple documents. You need to pass an array of documents for this method.

Example

The following example inserts three different documents using the insertMany() method in the empDetails collection.

> db.empDetails.insertMany(
	[
		{
			First_Name: "anshu",
			Last_Name: "verma",
			Date_Of_Birth: "1997-07-05",
			e_mail: "[email protected]",
			phone: "9054387510"
		},
		{
			First_Name: "michel",
			Last_Name: "john",
			Date_Of_Birth: "1992-01-14",
			e_mail: " [email protected]",
			phone: "9054385510"
		},
		{
			First_Name: "sadia",
			Last_Name: "sultan",
			Date_Of_Birth: "1994-06-21",
			e_mail: " [email protected]",
			phone: "9054383510"
		}
	]
)
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("5dd631f270fb13eec3963bed"),
		ObjectId("5dd631f270fb13eec3963bee"),
		ObjectId("5dd631f270fb13eec3963bef")
	]
}
>

No Sidebar ads