Inserting Documents into MongoDB Collections: A Step-by-Step Guide


Insert a Single Record into the Collections with Id

In MongoDB, when you insert a document into a collection without explicitly specifying an _id field, MongoDB will automatically generate a unique _id for that document. However, if you want to insert a single record into a collection with a specific _id value, you can do so by including the _id field in your document.

Syntax

db.collectionName.insertOne()

Here's how to insert a single record with a specific _id into a MongoDB collection:

db.students.insertOne({
    "_id":1,
    "Name":"Sam",
    "Age":23,
    "Gender":"Male",
    "City":"Salem"
})

Output

Record Single

insertOne({ "_id": 1, "Name": "Sam", "Age": 23, "Gender": "Male", "City": "Salem" }): This is the insertOne() method call, which is used to insert a single document into the collection. It includes an object (document) as an argument, and this document represents the data you want to insert

  • "_id": 1: This field specifies the unique identifier (ID) for the document. In this case, you've explicitly set it to 1. MongoDB will use this value as the document's primary key. If you insert another document with the same _id, it will overwrite the existing document with that ID.
  • "Name": "Sam": This field contains the name of the student, which is "Sam."
  • "Age": 23: This field represents the age of the student, which is 23.
  • "Gender": "Male": This field indicates the gender of the student, which is "Male."
  • "City": "Salem": This field specifies the city where the student resides, which is "Salem."

Once you run this MongoDB shell command, it will insert the provided document into the "students" collection with the specified fields and values. The document will have an _id of 1.

In summary, this code inserts a single student record with specific information into the "students" collection, and it assigns an explicit _id value of 1 to the document. The _id field is important because it ensures the uniqueness of documents within the collection and serves as the primary key for retrieval and update operations.

Insert a Single Record into the Collections without Id(It is Self-Generated)

In MongoDB, if you want to insert a single record into a collection without specifying a value for the _id field, MongoDB will automatically generate a unique _id for that document. You can achieve this by simply omitting the _id field when inserting the document.

Assuming you have a collection named "mycollection" and you want to insert a document without specifying an _id:

db.students.insertOne({
    "Name":"Sam",
    "Age":23,
    "Gender":"Male",
    "City":"Salem"
})

Output

Record Single

Insert a Multiple Record into the Collections without Id(It is Self-Generated)

To insert multiple records into a MongoDB collection without specifying an explicit _id (thus allowing MongoDB to generate unique _id values automatically), you can use the insertMany() method.

db.collectionName.insertMany()

Assuming you have a collection named "students," and you want to insert multiple documents without specifying _id values:

db.students.insertMany([
    {
        "Name":"Sam",
        "Age":23,
        "Gender":"Male",
        "City":"Salem"
    },
    {
        "Name":"Ram",
        "Age":33,
        "Gender":"Male",
        "City":"Coimbatore"
    },
    {
        "Name":"Sara",
        "Age":21,
        "Gender":"Female",
        "City":"Chennai"
    }
])

Output

Record Multiple

Insert a Multiple Record into the Collections with Id

To insert multiple records into a MongoDB collection with explicitly specified _id values, you can use the insertMany() method and include the _id field in each document.

db.collectionName.insertMany()

Assuming you have a collection named "student," and you want to insert multiple documents with specific _id values

db.students.insertMany([
    {
        "_id":1,
        "Name":"Sam",
        "Age":23,
        "Gender":"Male",
        "City":"Salem"
    },
    {
        "_id":2,
        "Name":"Ram",
        "Age":33,
        "Gender":"Male",
        "City":"Coimbatore"
    },
    {
        "_id":3,
        "Name":"Sara",
        "Age":21,
        "Gender":"Female",
        "City":"Chennai"
    }
])

Output

Record Multiple

we've provided an array of documents to the insertMany() method, and each document includes an _id field with a specific value. MongoDB will use these specified _id values for the inserted documents.

Please replace "student" with the actual name of your collection, and adjust the _id values and other document data as needed for your specific use case. Keep in mind that each _id value must be unique within the collection to avoid conflicts.