Introduction
Document databases store data in flexible, easy-to-understand formats. They let you work with data that changes often or has many parts without strict rules.
Jump into concepts and practice - no test required
Document databases store data in flexible, easy-to-understand formats. They let you work with data that changes often or has many parts without strict rules.
db.collection.insertOne({ key: value, key2: value2, ... })db.users.insertOne({ name: "Alice", age: 30, hobbies: ["reading", "hiking"] })db.orders.insertOne({ orderId: 123, items: [{ product: "Book", qty: 2 }, { product: "Pen", qty: 5 }] })This example creates a product document in the 'products' collection and then retrieves it to show the stored data.
use shopDB
db.products.insertOne({ name: "Coffee Mug", price: 12.99, tags: ["kitchen", "drinkware"], stock: 100 })
const product = db.products.findOne({ name: "Coffee Mug" })
printjson(product)Document databases are great for flexible and evolving data but may not enforce strict relationships like relational databases.
They often provide faster reads and writes for certain types of data because they avoid complex joins.
Document databases store data as flexible JSON-like documents.
They are useful when data structure changes or is complex.
They simplify storing related data together and scaling databases.
users?insertOne() to add a single document to a collection.db.users.insertOne({name: 'Alice', age: 30}), which is correct MongoDB syntax.products collection:{ "_id": 1, "name": "Pen", "details": { "color": "blue", "price": 1.5 } }db.products.find({"details.color": "blue"}) return?db.users.find({age: > 25}){age: {$gt: 25}}, not using > directly.