Document-based NoSQL databases store data in the form of documents, usually using JSON, BSON, or XML format. Each document is a self-contained unit that can contain nested fields and arrays, making it highly flexible for storing semi-structured data.
These systems are ideal for applications where the schema can vary between records, such as:
- Content management systems
- User profile data
- E-commerce catalogs
Popular Document-Based NoSQL Databases
- MongoDB
- CouchDB
- Amazon DocumentDB
Key Features
- Schema-less (no fixed structure)
- Easy to store complex/nested data
- High scalability
- Data stored as collections (like tables) of documents (like rows)
CRUD Operations in MongoDB
In MongoDB, the main operations are:
1. Create (Insert)
Insert a new document into a collection:
db.users.insertOne({
name: "John Doe",
age: 30,
email: "john@example.com"
});
Creates a document in the users collection.
2. Read (Query/Find)
Fetch all users:
db.users.find();
Find users with age > 25:
db.users.find({ age: { $gt: 25 } });
3. Update
Update one document:
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 31 } }
);
Update multiple documents:
db.users.updateMany(
{ age: { $lt: 25 } },
{ $set: { status: "young" } }
);
4. Delete
Delete one user:
db.users.deleteOne({ name: "John Doe" });
Delete multiple users with age > 60:
db.users.deleteMany({ age: { $gt: 60 } });
Summary Table
| Operation | MongoDB Command | Description |
|---|---|---|
| Create | insertOne(), insertMany() | Adds documents to a collection |
| Read | find(), findOne() | Retrieves documents from a collection |
| Update | updateOne(), updateMany() | Modifies existing documents |
| Delete | deleteOne(), deleteMany() | Removes documents from a collection |
