In this bankruptcy, we are able to discover ways to insert document in MongoDB series.
The insert() Method
To insert statistics into MongoDB series, you need to apply MongoDB's insert() or save() approach.
Syntax
The primary 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: "tutorials point",
... url: "http://www.tutorialspoint.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
>
Here mycol is our collection name, as created within the preceding chapter. If the gathering does not exist inside the database, then MongoDB will create this series after which insert a report into it.
In the inserted document, if we do not specify the _id parameter, then MongoDB assigns a completely unique ObjectId for this record.
_id is 12 bytes hexadecimal range specific for each report in a set. 12 bytes are divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
You also can pass an array of files into the insert() method as shown underneath:.
> db.createCollection("post")
> db.post.insert([
{
title: "MongoDB Overview",
description: "MongoDB is no SQL database",
by: "tutorials point",
url: "http://www.tutorialspoint.com",
tags: ["mongodb", "database", "NoSQL"],
likes: 100
},
{
title: "NoSQL Database",
description: "NoSQL database doesn't have tables",
by: "tutorials point",
url: "http://www.tutorialspoint.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 the document you could use db.Publish.Keep(report) also. If you do not specify _id within the report then save() approach will paintings equal as insert() technique. If you specify _id then it's going to update complete statistics of record containing _id as specified in store() approach.
The insertOne() technique
If you need to insert most effective one record into a set you could use this technique.
Syntax
The simple syntax of insert() command is as follows −
>db.COLLECTION_NAME.insertOne(document)
Example
Following example creates a brand new collection named empDetails and inserts a record the usage of the insertOne() approach.
> db.createCollection("empDetails")
{ "ok" : 1 }
> db.empDetails.insertOne(
{
First_Name: "Radhika",
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26",
e_mail: "radhika_sharma.123@gmail.com",
phone: "9848022338"
})
{
"acknowledged" : true,
"insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
}
>
The insertMany() technique
You can insert a couple of files using the insertMany() approach. To this method you need to bypass an array of documents.
Example
Following instance inserts 3 distinct files into the empDetails collection the usage of the insertMany() approach.
> db.empDetails.insertMany(
[
{
First_Name: "Radhika",
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26",
e_mail: "radhika_sharma.123@gmail.com",
phone: "9000012345"
},
{
First_Name: "Rachel",
Last_Name: "Christopher",
Date_Of_Birth: "1990-02-16",
e_mail: "Rachel_Christopher.123@gmail.com",
phone: "9000054321"
},
{
First_Name: "Fathima",
Last_Name: "Sheik",
Date_Of_Birth: "1990-02-16",
e_mail: "Fathima_Sheik.123@gmail.com",
phone: "9000054321"
}
]
)
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5dd631f270fb13eec3963bed"),
ObjectId("5dd631f270fb13eec3963bee"),
ObjectId("5dd631f270fb13eec3963bef")
]
}
>