Last Updated: 14 August, 2023
In MongoDB, a Document is nothing but a record, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.
In MongoDB, we can create or insert one or more documents into the collection using the following methods:
Method Name | Description |
---|---|
db.collection.insertOne() | Used to insert a single document into a collection. |
db.collection.insertMany() | Used to insert multiple documents into a collection. |
db.collection.insert() | Used to insert a single or multiple documents into a collection. |
In the insert operation, if the collection does not exist, a new collection will be created and the document will be inserted into it.
In MongoDB, all the insert operations target only a single collection.
In MongoDB, All write operations are atomic at the level of a single document.
In MongoDB, each document uniquly identitied by a unique _id field, this field acts as a primary key. In the insert operation, if we omit the _id field, the MongoDB driver automatically generates an ObjectID for the _id field.
_id is a 12 bytes hexadecimal number that is unique for every 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)
In MongoDB, the insertOne() method is used to insert or create a single document into a collection. This method was added in 3.2 version of MongoDB.
Let's see the examples of inserting a single documents in MongoDB.
db.student.insertOne( {sid: 1, name: 'Ramesh', class: 'Nursery', roll: 15, gender: 'M'} );
After inserting the document successful into the collection, we will get below output.
Output
{ "acknowledged" : true, "insertedId" : ObjectId("64ccbfeb3e4e00724be54359") }
Here, we have not specify the _id field, mongod creates and adds the _id field and assigns it a unique ObjectId() value.
Insert a Document with Specifying an _id Field
db.student.insertOne( {_id: 201, sid: 2, name: 'Ganesh', class: 'LKG', roll: 11, gender: 'M'} );
After inserting the document successful into the collection, we will get below output.
Output
{ "acknowledged" : true, "insertedId" : 201) }
MongoDB - Embedded Documents
Documents Embedded documents are those types of documents that contain a document inside another document.
When a collection has a document that contains another document, another document contains another sub-document, and so on, such types of documents are known as embedded or nested documents.
Notes:
In MongoDB, we can only nest documents up to 100 levels.
The Document size must not exceed 16 MB.
See the example below how to insert embedded document in MongoDB.
db.student.insertOne( {sid: 1, name: 'Ramesh', class: 'Nursery', roll: 15, gender: 'M', address: {area: 'Mathikere Road', city: 'Bengaluru', pin: 560054} });
Here, we are inserting one document inside another document. Actually it is making a One-to-One relationship. See the output below.
Output
{ "acknowledged" : true, "insertedId" : ObjectId("64ccc02c3e4e00724be5435b") }
Inserting an array of values into a field of a document
In MongoDB, we can store one value or an array of values in a field of a document. Let's see the example below.
db.student.insertOne( {sid: 1, name: 'Umesh', class: 'UKG', roll: 11, gender: 'M', marks: [45, 68, 51, 66]});
In this document, we are inserting mark data into the form array and inserting more than one value in the field.
Output
{ "acknowledged" : true, "insertedId" : ObjectId("64ccc00f3e4e00724be5435a") }
In MongoDB, insertMany() method is used to insert or create multiple documents into a collection. We pass array of documents to the method.
See the examples below.
db.student.insertMany( [{sid: 1, name: 'Ramesh', class: 'Nursery', roll: 15, gender: 'M'}, {sid: 2, name: 'Gyatri', class: 'LKG', roll: 18, gender: 'F'}, {sid: 3, name: 'John', class: 'Nursery', roll: 26, gender: 'M'}, {sid: 4, name: 'Priya', class: 'UKG', roll: 10, gender: 'F'} ]);
Output
{ "acknowledged" : true, "insertedIds" : [ ObjectId("64ccbdfb3e4e00724be54355"), ObjectId("64ccbdfb3e4e00724be54356"), ObjectId("64ccbdfb3e4e00724be54357"), ObjectId("64ccbdfb3e4e00724be54358") ] }
Here, we are inserting four documents together into a collection. In all four documents, we have not specified the _id field, so in the output, It is showing four unique ObjectIds.
In MongoDB, we can insert one or more documents together into a collection using the insert() method. See the examples.
Inserting Single Document
db.student.insert({name: 'Ramesh', class: 'Nursery', roll: 15});
Output
{ "acknowledged" : true, "insertedId" : ObjectId("64ccc00f3e4e00724be5435a") }
Inserting Multiple Documents
db.student.insert( [{sid: 1, name: 'Ramesh', class: 'Nursery', roll: 15, gender: 'M'}, {sid: 2, name: 'Gyatri', class: 'LKG', roll: 18, gender: 'F'}, {sid: 3, name: 'John', class: 'Nursery', roll: 26, gender: 'M'}, {sid: 4, name: 'Priya', class: 'UKG', roll: 10, gender: 'F'} ]);
Output
{ "acknowledged" : true, "insertedIds" : [ ObjectId("64ccbdfb3e4e00724be54355"), ObjectId("64ccbdfb3e4e00724be54356"), ObjectId("64ccbdfb3e4e00724be54357"), ObjectId("64ccbdfb3e4e00724be54358") ] }
Reference: https://www.mongodb.com/docs/mongodb-shell/crud/insert/
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com