MongoDB — Documents
Document Structure
{
"_id": ObjectId("..."),
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York"
},
"hobbies": ["reading", "gaming"]
}
Insert Document
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 30
})
Insert Multiple
db.users.insertMany([
{ name: "Jane", email: "jane@example.com" },
{ name: "Bob", email: "bob@example.com" }
])
Find Document
db.users.findOne({ name: "John" })
db.users.find({ age: { $gt: 25 } })
Update Document
db.users.updateOne(
{ name: "John" },
{ $set: { age: 31 } }
)
Delete Document
db.users.deleteOne({ name: "John" })
Mini Practice
- Insert a document
- Find documents
- Update a document
- Delete a document
Up Next
Continue with Data Types — BSON data types.
Related Topics
Frequently Asked Questions about Documents
What is Documents in MongoDB?
Documents is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Documents?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Documents.
Why is Documents important in MongoDB?
Documents is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.