...

Database Managment - Mongo DB

Back to Course

Lesson Description


Lession - #1410 MongoDB Insert Document


MongoDB - Insert Document

The addition(>
Method
To embed information into MongoDB assortment, you want to utilize MongoDB's addition(>
or save(>
strategy. Linguistic structure The essential punctuation of addition(>
order is as per the following −

>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 assortment name, as made in the past part. On the off chance that the assortment doesn't exist in the information base, then MongoDB will make this assortment and afterward embed an archive into it. In the embedded archive, on the off chance that we don't determine the _id boundary, then MongoDB appoints an exceptional ObjectId for this report. id is 12 bytes hexadecimal number extraordinary for each report in an assortment. 12 bytes are partitioned as follows −

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

You can likewise pass a variety of reports into the supplement(>
strategy as displayed 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 embed the record you can utilize db.post.save(document>
moreover. In the event that you don't indicate _id in the archive then, at that point, save(>
technique will work same as addition(>
strategy. In the event that you determine _id, it will supplant entire information of archive containing _id as indicated in save(>
technique.

The insertOne(>
method

Assuming you want to embed just a single report into an assortment you can utilize this technique. Syntax The essential language structure of supplement(>
order is as per the following −

>db.COLLECTION_NAME.insertOne(document>

Example: Following model makes another assortment named empDetails and embeds a report utilizing the insertOne(>
strategy.

> 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(>
method

You can embed various reports utilizing the insertMany(>
technique. To this strategy you want to pass a variety of archives.
Example Following model supplements three distinct archives into the empDetails assortment utilizing the insertMany(>
technique.

> 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">
] } >