Collections and Methods
In MongoDB, collections are used to organize documents. A collection can be thought of as a container or group used to store documents of similar structure, like a table in relational databases. However, unlike tables, collections don’t enforce a strict schema, offering more flexibility in managing your data.
Key Features
- Flexible Schema: A collection can contain multiple documents with different structures or fields, allowing you to store unstructured or semi-structured data.
- Dynamic: Collections can be created implicitly or explicitly, and documents can be added or removed easily without affecting others in the collection.
Creating Collections
To create a collection in MongoDB, you can choose from two methods:
Implicit Creation: When you insert a document without specifying an existing collection, MongoDB automatically creates the collection for you.
db.createCollection('users');
Explicit Creation: Use the
db.createCollection(name, options)
method to create a collection with specific options:db.createCollection('users', { capped: true, size: 100000, max: 5000 });
Managing Collections
Insert Documents: To insert a document into a collection, use the
insertOne()
orinsertMany()
methods.db.users.insertOne({ name: 'John Doe', age: 30, email: 'john@example.com' }); db.users.insertMany([ { name: 'Jane Doe', age: 28, email: 'jane@example.com' }, { name: 'Mary Jane', age: 32, email: 'mary@example.com' }, ]);
Find Documents: Use the
find()
method to query documents in a collection.db.users.find({ age: { $gt: 30 } });
Update Documents: Use the
updateOne()
,updateMany()
, orreplaceOne()
methods to modify documents in a collection.db.users.updateOne({ name: 'John Doe' }, { $set: { age: 31 } }); db.users.updateMany({ age: { $gt: 30 } }, { $inc: { age: 1 } });
Delete Documents: Use the
deleteOne()
ordeleteMany()
methods to remove documents from a collection.db.users.deleteOne({ name: 'John Doe' }); db.users.deleteMany({ age: { $lt: 30 } });
Drop Collection: To delete the entire collection, use the
drop()
method.db.users.drop();
In summary, collections are an essential part of MongoDB that enable you to efficiently manage and store documents with varying structures. Their flexible schema and dynamic nature make them perfect for handling both unstructured and semi-structured data.