MongoDB Documents and BSON Practice Questions with Solutions

Introduction

In MongoDB, data is stored as documents inside collections. Documents use a JSON-like format, while MongoDB actually stores them internally using BSON (Binary JSON). BSON supports additional data types such as ObjectId, Date, Decimal128, and binary data. In this chapter, you will practice creating documents, working with different BSON data types, accessing fields, and understanding how MongoDB represents document data. MongoDB Documents and BSON practice questions with solutions to help you understand the concepts.

Q1. Insert a basic MongoDB document

Problem Statement

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

MongoDB Command / Query

use bson_practice

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

Expected Output

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

Explanation

A MongoDB document is written using a JSON-like structure:

{
    name: "Rahul",
    age: 15,
    course: "Python"
}

MongoDB automatically adds an _id field when one is not provided.


Q2. Insert a document with different data types

Problem Statement

Insert a student document containing a string, integer, Boolean value, array, and nested object.

MongoDB Command / Query

db.students.insertOne({
    name: "Priya",
    age: 16,
    active: true,
    skills: ["Python", "SQL", "HTML"],
    address: {
        city: "Delhi",
        country: "India"
    }
})

Expected Output

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

Explanation

A MongoDB document can contain different types of values.

Here:

name     → String
age      → Number
active   → Boolean
skills   → Array
address  → Embedded Document

This flexibility is one of the important features of MongoDB.


Q3. Create your own ObjectId

Problem Statement

Insert a document while manually generating its _id using MongoDB’s ObjectId() BSON type.

MongoDB Command / Query

db.students.insertOne({
    _id: ObjectId(),
    name: "Aman",
    age: 17,
    course: "Data Analytics"
})

Expected Output

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

Explanation

ObjectId() creates a MongoDB ObjectId value.

MongoDB commonly uses ObjectId for the _id field because every document in a collection needs a unique _id.


Q4. Store a date using BSON Date

Problem Statement

Insert a course document with a startDate using MongoDB’s BSON Date type.

MongoDB Command / Query

db.courses.insertOne({
    name: "MongoDB",
    duration: "3 Months",
    startDate: ISODate("2026-09-15")
})

Expected Output

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

Explanation

ISODate() creates a BSON Date value in mongosh.

This is different from storing a date as a normal string:

startDate: "2026-09-15"

Using a BSON Date allows MongoDB to treat the value as a date for date-based operations and queries.


Q5. Insert an array inside a document

Problem Statement

Store a student’s programming skills as an array.

MongoDB Command / Query

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

Expected Output

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

Explanation

MongoDB supports arrays directly inside documents.

The skills field contains three values:

Python
MongoDB
JavaScript

You can later query documents based on values inside an array.


Q6. Insert a nested document

Problem Statement

Store a student’s address as an embedded document inside the student document.

MongoDB Command / Query

db.students.insertOne({
    name: "Rohit",
    age: 15,
    address: {
        city: "Delhi",
        area: "Dwarka",
        pincode: 110075
    }
})

Expected Output

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

Explanation

The address field contains another document:

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

This is called an embedded document or nested document.

MongoDB allows documents to contain other documents, which makes it easy to represent related data together.


Q7. Find documents containing a specific BSON data type

Problem Statement

Find students whose age field is stored as a number.

MongoDB Command / Query

db.students.find({
    age: {
        $type: "number"
    }
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: "Rahul",
    age: 15,
    course: "Python"
  },
  {
    _id: ObjectId('...'),
    name: "Priya",
    age: 16,
    active: true,
    skills: ["Python", "SQL", "HTML"],
    address: {
      city: "Delhi",
      country: "India"
    }
  }
]

Explanation

The $type operator allows you to query documents according to the BSON type of a field.

For example:

{
    age: {
        $type: "number"
    }
}

means that MongoDB should find documents where age is a numeric value.


Q8. Access a field inside a nested document

Problem Statement

Find students whose address is located in Delhi.

MongoDB Command / Query

db.students.find({
    "address.city": "Delhi"
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: "Priya",
    age: 16,
    active: true,
    skills: ["Python", "SQL", "HTML"],
    address: {
      city: "Delhi",
      country: "India"
    }
  },
  {
    _id: ObjectId('...'),
    name: "Rohit",
    age: 15,
    address: {
      city: "Delhi",
      area: "Dwarka",
      pincode: 110075
    }
  }
]

Explanation

The dot notation:

"address.city"

allows MongoDB to access a field inside a nested document.

The structure is:

address
   └── city

Q9. Find documents containing a particular value in an array

Problem Statement

Find all students who have "MongoDB" in their skills array.

MongoDB Command / Query

db.students.find({
    skills: "MongoDB"
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: "Neha",
    age: 16,
    skills: ["Python", "MongoDB", "JavaScript"]
  }
]

Explanation

MongoDB can search an array field directly.

You do not need to write a special loop. MongoDB checks the values inside the skills array and returns documents containing "MongoDB".


Q10. View the BSON type of a value

Problem Statement

Use mongosh to check the BSON type of the _id value of a document.

MongoDB Command / Query

First retrieve a document:

db.students.findOne()

Suppose the result contains:

_id: ObjectId('...')

You can inspect the type using:

typeof db.students.findOne()._id

Expected Output

object

For MongoDB-specific BSON type information, you can also inspect the value directly:

db.students.findOne()._id

Example:

ObjectId('68c2...')

Explanation

MongoDB’s ObjectId is a BSON type, not an ordinary string.

For example:

"68c2..."

is a string, while:

ObjectId("68c2...")

is an ObjectId BSON value.

This distinction becomes important when querying documents by _id.

Key Takeaways

  • MongoDB stores data as documents.
  • Documents use a JSON-like syntax, but MongoDB stores them as BSON.
  • BSON supports more data types than standard JSON.
  • MongoDB commonly uses ObjectId for the _id field.
  • ISODate() can be used to create BSON Date values in mongosh.
  • Documents can contain arrays.
  • Documents can contain nested/embedded documents.
  • Dot notation such as "address.city" accesses nested fields.
  • $type can be used to query documents based on BSON data types.
  • MongoDB can search values directly inside arrays.
  • BSON types are important when storing and querying MongoDB data.

FAQs

1. What is a document in MongoDB?

A document is a set of field-value pairs that MongoDB stores inside a collection. For example:

{
    name: "Rahul",
    age: 15,
    course: "Python"
}

2. What is BSON in MongoDB?

BSON stands for Binary JSON. It is the binary-encoded format MongoDB uses to store documents. BSON supports additional data types such as ObjectId, Date, Decimal128, and binary data.

3. Is MongoDB document the same as JSON?

Not exactly. MongoDB documents have a JSON-like syntax, but MongoDB stores them as BSON. BSON supports data types that standard JSON does not, such as ObjectId and BSON Date.

4. What is ObjectId in MongoDB?

ObjectId is a BSON data type commonly used as the default value for a document’s _id field. MongoDB generates an ObjectId automatically when you insert a document without specifying _id.

5. Can a MongoDB document contain another document?

Yes. MongoDB supports embedded or nested documents.

{
    name: "Rohit",
    address: {
        city: "Delhi",
        area: "Dwarka"
    }
}

6. Can MongoDB documents contain arrays?

Yes. Arrays are supported directly inside MongoDB documents.

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

7. Why is BSON used instead of normal JSON?

BSON provides efficient binary encoding and supports additional data types that JSON does not natively provide. This makes it suitable for MongoDB’s document storage and querying system.

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

Scroll to Top