MongoDB Insert Documents Practice Questions with Solutions

Introduction

In MongoDB, the insert operations are used to add new documents to a collection. You can insert a single document, multiple documents, custom _id values, nested objects, arrays, and different data types. In this chapter, you will practice the most important MongoDB document insertion techniques using insertOne() and insertMany(), with practical examples that are useful for real-world MongoDB applications. MongoDB Insert Documents Practice Questions with Solutions to help you understand the concepts.

Q1. Insert one document using insertOne()

Problem Statement

Create a students collection and insert one student document containing the student’s name, age, and course.

MongoDB Command / Query

use insert_practice

db.students.insertOne({
    name: "Rahul",
    age: 15,
    course: "Python"
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

insertOne() is used when you want to insert one document into a collection.

MongoDB automatically creates the _id field if you do not provide one.


Q2. Insert multiple documents using insertMany()

Problem Statement

Insert three students into the students collection at the same time.

MongoDB Command / Query

db.students.insertMany([
    {
        name: "Aman",
        age: 16,
        course: "JavaScript"
    },
    {
        name: "Priya",
        age: 15,
        course: "Python"
    },
    {
        name: "Neha",
        age: 17,
        course: "MongoDB"
    }
])

Expected Output

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('...'),
    '1': ObjectId('...'),
    '2': ObjectId('...')
  }
}

Explanation

insertMany() allows you to insert multiple documents in one operation.

This is useful when you have several records that need to be added together.


Q3. Insert a document with a custom _id

Problem Statement

Insert a student document and manually assign student001 as its _id.

MongoDB Command / Query

db.students.insertOne({
    _id: "student001",
    name: "Rohit",
    age: 16,
    course: "SQL"
})

Expected Output

{
  acknowledged: true,
  insertedId: "student001"
}

Explanation

MongoDB normally generates an ObjectId automatically for _id.

However, you can provide your own unique _id value.

Here, the _id is a string:

_id: "student001"

The _id value must be unique within the collection.


Q4. Insert a document containing an array

Problem Statement

Insert a student document containing multiple programming skills in an array.

MongoDB Command / Query

db.students.insertOne({
    name: "Karan",
    age: 17,
    skills: [
        "Python",
        "MongoDB",
        "JavaScript"
    ]
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

MongoDB documents can contain arrays.

The skills field contains three values:

Python
MongoDB
JavaScript

Q5. Insert a document containing a nested object

Problem Statement

Insert a student document with an embedded address containing city and pincode.

MongoDB Command / Query

db.students.insertOne({
    name: "Simran",
    age: 15,
    course: "Data Analytics",
    address: {
        city: "Delhi",
        pincode: 110075
    }
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

MongoDB allows one document to contain another document.

Here, address is an embedded document:

address: {
    city: "Delhi",
    pincode: 110075
}

Q6. Insert a document with Boolean and Date values

Problem Statement

Insert a course document containing a Boolean field and a BSON Date.

MongoDB Command / Query

db.courses.insertOne({
    name: "MongoDB",
    active: true,
    startDate: ISODate("2026-09-20")
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

The document contains two different BSON data types:

active    → Boolean
startDate → Date

Using ISODate() in mongosh creates a BSON Date value rather than an ordinary text string.


Q7. Insert multiple documents with ordered insertion

Problem Statement

Insert three course documents using insertMany() with ordered insertion.

MongoDB Command / Query

db.courses.insertMany(
    [
        {
            name: "Python",
            duration: "3 Months"
        },
        {
            name: "SQL",
            duration: "2 Months"
        },
        {
            name: "MongoDB",
            duration: "2 Months"
        }
    ],
    {
        ordered: true
    }
)

Expected Output

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('...'),
    '1': ObjectId('...'),
    '2': ObjectId('...')
  }
}

Explanation

The option:

{
    ordered: true
}

tells MongoDB to process the documents in the given order.

If an error occurs during an ordered bulk insertion, MongoDB stops processing subsequent documents in that operation.

ordered: true is also the default behavior for insertMany().


Q8. Insert multiple documents with unordered insertion

Problem Statement

Insert several documents using insertMany() and tell MongoDB to continue processing other documents if one insertion causes an error.

MongoDB Command / Query

db.students.insertMany(
    [
        {
            _id: "S101",
            name: "Arjun",
            age: 16
        },
        {
            _id: "S102",
            name: "Meera",
            age: 15
        },
        {
            _id: "S103",
            name: "Vikas",
            age: 17
        }
    ],
    {
        ordered: false
    }
)

Expected Output

{
  acknowledged: true,
  insertedIds: {
    '0': "S101",
    '1': "S102",
    '2': "S103"
  }
}

Explanation

With:

{
    ordered: false
}

MongoDB does not stop the entire insertion sequence just because one document encounters an error.

This can be useful when inserting many independent documents and you want MongoDB to continue processing the remaining documents.


Q9. Insert a document with mixed data types

Problem Statement

Insert a product document containing a string, number, Boolean, array, nested document, and Date.

MongoDB Command / Query

db.products.insertOne({
    name: "Laptop",
    price: 55000,
    available: true,
    tags: [
        "computer",
        "electronics"
    ],
    seller: {
        name: "Tech Store",
        city: "Delhi"
    },
    addedOn: ISODate("2026-09-12")
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

A MongoDB document can contain different BSON-supported values in the same document.

This example combines:

name      → String
price     → Number
available → Boolean
tags      → Array
seller    → Embedded Document
addedOn   → Date

This flexibility is one of MongoDB’s important characteristics.


Q10. Verify inserted documents

Problem Statement

After inserting several documents into the students collection, retrieve them to verify that the insertion was successful.

MongoDB Command / Query

db.students.find()

Expected Output

[
  {
    _id: ObjectId('...'),
    name: "Rahul",
    age: 15,
    course: "Python"
  },
  {
    _id: ObjectId('...'),
    name: "Aman",
    age: 16,
    course: "JavaScript"
  },
  {
    _id: ObjectId('...'),
    name: "Priya",
    age: 15,
    course: "Python"
  }
]

Explanation

After inserting documents, it is good practice to verify the data.

The find() method retrieves documents from the collection so you can confirm that the insertion worked correctly.

Key Takeaways

  • insertOne() inserts one document.
  • insertMany() inserts multiple documents.
  • MongoDB automatically generates _id when you do not provide one.
  • You can provide your own _id, but it must be unique within the collection.
  • Documents can contain arrays and nested documents.
  • MongoDB supports different BSON data types in the same document.
  • ISODate() can be used to insert Date values in mongosh.
  • ordered: true processes insertMany() documents in order and stops after an error.
  • ordered: false allows MongoDB to continue processing other documents after an error.
  • Use find() after insertion to verify your documents.

FAQs

1. What is insertOne() in MongoDB?

insertOne() is a MongoDB method used to insert a single document into a collection.

db.students.insertOne({
    name: "Rahul",
    age: 15
})

2. What is insertMany() in MongoDB?

insertMany() is used to insert multiple documents into a collection in a single operation.

db.students.insertMany([
    { name: "Rahul", age: 15 },
    { name: "Priya", age: 16 }
])

3. Does MongoDB automatically create an _id?

Yes. If you do not provide an _id, MongoDB automatically generates one, commonly using the ObjectId BSON type.

4. Can I create my own _id in MongoDB?

Yes. You can specify your own _id value:

db.students.insertOne({
    _id: "S101",
    name: "Aman"
})

The _id must be unique within that collection.

5. Can a MongoDB document contain arrays?

Yes. MongoDB documents can contain arrays of strings, numbers, objects, or other supported BSON values.

{
    name: "Neha",
    skills: ["Python", "SQL", "MongoDB"]
}

6. What is the difference between ordered and unordered insertion?

With ordered: true, MongoDB processes documents in order and stops processing the remaining documents after an error. With ordered: false, MongoDB can continue processing other documents even if one document encounters an error.

7. How can I verify that a document was inserted successfully?

You can use find() to retrieve the documents:

db.students.find()

You can also check the acknowledged value returned by the insertion operation.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top