MongoDB - Day 6 (Insert Method)

Before reading this article, I highly recommend reading the following previous parts of the series:

A write operation is any operation that creates and modifies data. A relational database contains 3 types of write operations. They are Insert, Update and Delete. MongoDB has a number of write operations (in other words Insert, Update, save, Delete, upsert ) that are insert, update and remove data. Each write operation targets a single collection. Each write operation is atomic at the document level.

To remove, save, update and insert operations, we can specify the criteria, or conditions, that identify the documents to update or remove.

This article explains the insert method.

Insert

The Insert method in MongoDB is similar to the “Insert Into” command of Relational Databases. The Insert method inserts a new document into a collection. We can insert single, multiple and embedded documents into collections depending on our preverences or requirements.

Syntax: db.Collection_Name.insert()

Insert Single Document

Insert Single Document

The preceding diagram shows a basic method to insert a single document into a collection. Now we execute some query in the MongoDB shell.

MongoDB shell

In the preceding example we insert 3 documents but only a single document at a time is in the Employee collection.

Insert Multiple Documents

In previous example we saw how to insert a single document into a collection. This approach will take time if we have bulk data to insert. For such types, MongoDB provides a method for multiple insertions. Using this method we can insert multiple documents in a single insert command.

Insert Multiple Document

Let us see an example as in the following:

see an example

In the preceding example we insert two documents using a single insert command.

Insert Embedded Document

A document that contains another document as it's key-value pair is known as an Embedded Document. A MongoDB database has a flexible schema so there is no need to have the same set of fields or structures and common fields in a collection document. The following image provide a basic idea of an embedded document structure.

Document structure

Let us see an example as in the following:

Example

Insert_Behaviour

When we add a new document in any collection without specifying the _id field, then MongoDB adds an _id field implicitly. This _Id field uniquely identifies each document. If we provide the _id field explicitly then MongoDB first checks the value of the _Id field . If this _id already exists then MongoDB will throw an exception.

Let us see an example as in the following:

Insert Behaviour

In the preceding example we add two documents. In the first document we didn't specify the _id field, so MongoDB internally adds the _id to the document. In the second document we provide an external _id field.

Take another example as in the following:

add two document

In the preceding example we try to insert a new document into a collection. But the value of the _id field already exists so the MongoDB database throws the error that the ObjectId already exists.

Embedded Document as Alternate of Joins

In a relational database we use Join to retrieve data from two or more tables. But in MongoDB we don't need a Join, we can use the concept of an embedded document.

Let us see an example of Social Networking Sites that have the following requirements:

  • A user can publish any number of posts.
  • Every post contains a total number of likes.
  • Every post has comments given by other users along with their userId, message, DateTime and like option.
  • Each post can have zero or more comments.
  • A user can comment zero or more times for a post

For such type of scenario in a Relational Database we require two tables and some relationship between the tables.

Relational Database

If you want to retrieve information about a post then we must do Join operations as in the following:

perform Join operations

But in MongoDB we can do that using an Embedded Document. We are not required to maintain a relationship and do any Join operations as in the following:

Embedded Document

If we want to retrieve information about a post then we can use the following query:

retrieve information about any post

We can see that if we want to insert and retrieve the same information then we require two tables and a Join operation in a relational database but we can do this using a single Collection in MongoDB and don't require Join operations.

In my next article I will explain other methods for write operations.

Up Next
    Ebook Download
    View all
    Learn
    View all