In MongoDB, data is stored in a document-oriented format, and these documents are represented in BSON (Binary JSON) format. The document structure is similar to a JSON object but with additional data types for efficient storage and processing.
Understanding the document structure is crucial for designing a flexible and efficient database schema in MongoDB. Documents are the primary unit of data storage in MongoDB, and each document is stored as an entry in a collection. Collections in MongoDB are akin to tables in relational databases, but collections can store documents of varying structures.
A document in MongoDB is a set of key-value pairs. The keys are strings, and the values can be a variety of data types, including strings, numbers, arrays, embedded documents, binary data, and more.
json
Copy code
{ "_id": ObjectId("60d5f8d8c174c83d5c8b53d5"), // Unique identifier (automatically generated if not provided) "name": "John Doe", // Field: key-value pair "age": 30, // Field: key-value pair "address": { // Embedded document (nested object) "street": "123 Main St", "city": "New York", "zip": "10001" }, "isActive": true, // Field: boolean value "hobbies": ["reading", "traveling"], // Field: array of values "createdAt": ISODate("2024-11-03T12:00:00Z") // Field: date }
_id: Every document in MongoDB has an _id field which is a unique identifier. If you don’t provide one, MongoDB generates an ObjectId automatically. You can customize the _id field if needed.MongoDB uses BSON (Binary JSON) as its data format. BSON is a binary-encoded serialization format that extends JSON’s flexibility by supporting additional data types such as ObjectId, Date, Binary data, and Decimal128. BSON allows MongoDB to store data efficiently while supporting high-performance queries.
ObjectId for unique identifiers, and Date for storing date values.ObjectId: A unique 12-byte identifier automatically created by MongoDB for the _id field.Date: Date objects used to store timestamps.Binary: Binary data, often used for storing files, images, or other binary objects.Decimal128: High-precision decimal values.MinKey and MaxKey: Special values used in range queries.Documents can contain many different types of fields, and MongoDB allows various data types as values.
"name": "John Doe""age": 30"isActive": true"createdAt": ISODate("2024-11-03T12:00:00Z")"tags": ["technology", "MongoDB", "NoSQL"]"address": { "street": "123 Main St", "city": "New York", "zip": "10001" }"_id": ObjectId("60d5f8d8c174c83d5c8b53d5")"middleName": null"profilePicture": BinData(0,"base64string")MongoDB allows you to nest documents within other documents, allowing you to model complex relationships in a single record. This approach is useful when related data is frequently accessed together and is not expected to change independently.
json
Copy code
{ "_id": ObjectId("60d5f8d8c174c83d5c8b53d5"), "name": "John Doe", "address": { "street": "123 Main St", "city": "New York", "zip": "10001" }, "orders": [ { "orderId": ObjectId("60d5f8d8c174c83d5c8b53d6"), "product": "Laptop", "quantity": 1, "price": 999.99 }, { "orderId": ObjectId("60d5f8d8c174c83d5c8b53d7"), "product": "Smartphone", "quantity": 2, "price": 499.99 } ] }
In this example, the address and orders are embedded documents within the users document. Embedding works best when the embedded data is tightly coupled to the parent document (e.g., a user’s orders or address).
Arrays allow you to store multiple values in a single field, which can be particularly useful for fields like tags, comments, or other lists of related data.
json
Copy code
{ "_id": ObjectId("60d5f8d8c174c83d5c8b53d5"), "name": "John Doe", "tags": ["developer", "MongoDB", "JavaScript"] }
Arrays can also contain nested documents:
json
Copy code
{ "_id": ObjectId("60d5f8d8c174c83d5c8b53d5"), "name": "John Doe", "orders": [ { "orderId": ObjectId("60d5f8d8c174c83d5c8b53d6"), "product": "Laptop", "quantity": 1, "price": 999.99 }, { "orderId": ObjectId("60d5f8d8c174c83d5c8b53d7"), "product": "Smartphone", "quantity": 2, "price": 499.99 } ] }
MongoDB provides several operators for modifying documents, such as $set, $unset, $push, $pull, and $addToSet. These operators allow you to update nested data, arrays, or individual fields within documents.
db.users.updateOne( { "_id": ObjectId("60d5f8d8c174c83d5c8b53d5") }, { $set: { "address.city": "Los Angeles" } } );db.users.updateOne( { "_id": ObjectId("60d5f8d8c174c83d5c8b53d5") }, { $push: { "tags": "MongoDB Advanced" } } );db.users.updateOne( { "_id": ObjectId("60d5f8d8c174c83d5c8b53d5") }, { $pull: { "tags": "JavaScript" } } );_id FieldMongoDB automatically generates a unique _id field for each document if you don’t specify one. The _id field is indexed, making it the default field used for searching or identifying documents.
_id Field:You can also provide your own _id field if you want to control the identifier, but it must be unique within the collection.
json
Copy code
{ "_id": "user123", "name": "John Doe" }