The MongoCollection class provides MongoDB collection operations.
More...
#include <QC_MongoCollection.dox.h>
|
| MongoCursor | aggregate (list< hash< auto > > pipeline) |
| | Runs an aggregation pipeline on the collection.
|
| |
| int | countDocuments (*hash< auto > filter) |
| | Counts the number of documents matching a filter.
|
| |
| hash< auto > | deleteMany (hash< auto > filter) |
| | Deletes multiple documents from the collection.
|
| |
| hash< auto > | deleteOne (hash< auto > filter) |
| | Deletes a single document from the collection.
|
| |
| bool | drop () |
| | Drops (deletes) this collection.
|
| |
| MongoCursor | find (*hash< auto > filter, *hash< auto > opts) |
| | Finds documents in the collection.
|
| |
| *hash< auto > | findOne (*hash< auto > filter) |
| | Finds a single document in the collection.
|
| |
| *string | getName () |
| | Returns the name of this collection.
|
| |
| hash< auto > | insertMany (list< hash< auto > > documents) |
| | Inserts multiple documents into the collection.
|
| |
| hash< auto > | insertOne (hash< auto > document) |
| | Inserts a single document into the collection.
|
| |
| hash< auto > | replaceOne (hash< auto > filter, hash< auto > replacement, *hash< auto > options) |
| | Replaces a single document in the collection.
|
| |
| hash< auto > | updateMany (hash< auto > filter, hash< auto > update, *hash< auto > options) |
| | Updates multiple documents in the collection.
|
| |
| hash< auto > | updateOne (hash< auto > filter, hash< auto > update, *hash< auto > options) |
| | Updates a single document in the collection.
|
| |
The MongoCollection class provides MongoDB collection operations.
This class represents a MongoDB collection and provides methods for CRUD operations, aggregation, and collection management.
- Example:
MongoCollection users = db.getCollection("users");
hash<auto> result = users.insertOne({"name": "John", "age": 30});
MongoCursor cursor = users.find({"age": {"$gt": 25}});
while (*hash<auto> doc = cursor.next()) {
printf("Found: %y\n", doc);
}
users.updateOne({"name": "John"}, {"$set": {"age": 31}});
users.deleteMany({"age": {"$lt": 18}});
- Since
- mongodb 1.0
◆ aggregate()
| MongoCursor mongodb::MongoCollection::aggregate |
( |
list< hash< auto > > |
pipeline | ) |
|
Runs an aggregation pipeline on the collection.
- Parameters
-
| pipeline | a list of pipeline stages |
- Returns
- a MongoCursor for iterating the results
- Example:
list<hash<auto>> pipeline = (
{"$match": {"status": "active"}},
{"$group": {"_id": "$category", "count": {"$sum": 1}}},
{"$sort": {"count": -1}}
);
MongoCursor cursor = coll.aggregate(pipeline);
while (*hash<auto> doc = cursor.next()) {
printf("%s: %d\n", doc._id, doc.count);
}
- Exceptions
-
| MONGODB-COLLECTION-ERROR | aggregation failed |
◆ countDocuments()
| int mongodb::MongoCollection::countDocuments |
( |
*hash< auto > |
filter | ) |
|
Counts the number of documents matching a filter.
- Parameters
-
| filter | the query filter (optional, empty hash counts all) |
- Returns
- the count of matching documents
- Example:
int total = coll.countDocuments();
int active = coll.countDocuments({"status": "active"});
- Exceptions
-
| MONGODB-COLLECTION-ERROR | count failed |
◆ deleteMany()
| hash< auto > mongodb::MongoCollection::deleteMany |
( |
hash< auto > |
filter | ) |
|
Deletes multiple documents from the collection.
- Parameters
-
| filter | the query filter to select documents |
- Returns
- a hash containing
deletedCount
- Example:
hash<auto> result = coll.deleteMany({"status": "obsolete"});
printf("Deleted %d documents\n", result.deletedCount);
- Exceptions
-
| MONGODB-COLLECTION-ERROR | delete failed |
◆ deleteOne()
| hash< auto > mongodb::MongoCollection::deleteOne |
( |
hash< auto > |
filter | ) |
|
Deletes a single document from the collection.
- Parameters
-
| filter | the query filter to select the document |
- Returns
- a hash containing
deletedCount
- Example:
hash<auto> result = coll.deleteOne({"_id": oid});
if (result.deletedCount > 0) {
print("Document deleted\n");
}
- Exceptions
-
| MONGODB-COLLECTION-ERROR | delete failed |
◆ drop()
| bool mongodb::MongoCollection::drop |
( |
| ) |
|
Drops (deletes) this collection.
- Returns
- True if the collection was dropped
- Example:
if (coll.drop()) {
print("Collection dropped\n");
}
- Exceptions
-
| MONGODB-COLLECTION-ERROR | drop failed |
◆ find()
| MongoCursor mongodb::MongoCollection::find |
( |
*hash< auto > |
filter, |
|
|
*hash< auto > |
opts |
|
) |
| |
Finds documents in the collection.
- Parameters
-
| filter | the query filter (optional, empty hash matches all) |
| opts | find options (optional): projection, sort, limit, skip |
- Returns
- a MongoCursor for iterating the results
- Example:
MongoCursor cursor = coll.find();
MongoCursor cursor = coll.find({"status": "active"});
MongoCursor cursor = coll.find({"status": "active"}, {
"projection": {"name": 1, "age": 1},
"sort": {"age": -1},
"limit": 10
});
- Exceptions
-
| MONGODB-COLLECTION-ERROR | find failed |
◆ findOne()
| *hash< auto > mongodb::MongoCollection::findOne |
( |
*hash< auto > |
filter | ) |
|
Finds a single document in the collection.
- Parameters
-
- Returns
- the first matching document, or nothing if no match
- Example:
*hash<auto> doc = coll.findOne({"_id": oid});
if (doc) {
printf("Found: %y\n", doc);
}
- Exceptions
-
| MONGODB-COLLECTION-ERROR | find failed |
◆ getName()
| *string mongodb::MongoCollection::getName |
( |
| ) |
|
Returns the name of this collection.
- Returns
- the collection name
- Code Flags:
- CONSTANT
- Example:
string name = coll.getName();
◆ insertMany()
| hash< auto > mongodb::MongoCollection::insertMany |
( |
list< hash< auto > > |
documents | ) |
|
Inserts multiple documents into the collection.
- Parameters
-
| documents | a list of documents to insert |
- Returns
- a hash containing the insert result with
insertedCount
- Example:
list<hash<auto>> docs = (
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
);
hash<auto> result = coll.insertMany(docs);
printf("Inserted %d documents\n", result.insertedCount);
- Exceptions
-
| MONGODB-COLLECTION-ERROR | insert failed |
◆ insertOne()
| hash< auto > mongodb::MongoCollection::insertOne |
( |
hash< auto > |
document | ) |
|
Inserts a single document into the collection.
- Parameters
-
| document | the document to insert |
- Returns
- a hash containing the insert result with
insertedId
- Example:
hash<auto> result = coll.insertOne({"name": "Alice", "age": 25});
printf("Inserted ID: %y\n", result.insertedId);
- Exceptions
-
| MONGODB-COLLECTION-ERROR | insert failed |
◆ replaceOne()
| hash< auto > mongodb::MongoCollection::replaceOne |
( |
hash< auto > |
filter, |
|
|
hash< auto > |
replacement, |
|
|
*hash< auto > |
options |
|
) |
| |
Replaces a single document in the collection.
- Parameters
-
| filter | the query filter to select the document |
| replacement | the replacement document (must not contain update operators) |
| options | optional replace options (e.g., upsert: True) |
- Returns
- a hash containing
matchedCount, modifiedCount, and optionally upsertedId
- Example:
hash<auto> result = coll.replaceOne(
{"_id": oid},
{"name": "new name", "value": 100}
);
printf("Replaced: %d\n", result.modifiedCount);
hash<auto> result = coll.replaceOne(
{"name": "test"},
{"name": "test", "value": 1, "created": now()},
{"upsert": True}
);
- Exceptions
-
| MONGODB-COLLECTION-ERROR | replace failed |
◆ updateMany()
| hash< auto > mongodb::MongoCollection::updateMany |
( |
hash< auto > |
filter, |
|
|
hash< auto > |
update, |
|
|
*hash< auto > |
options |
|
) |
| |
Updates multiple documents in the collection.
- Parameters
-
| filter | the query filter to select documents |
| update | the update operations |
| options | optional update options (e.g., upsert: True) |
- Returns
- a hash containing
matchedCount, modifiedCount, and optionally upsertedId
- Example:
hash<auto> result = coll.updateMany(
{"status": "pending"},
{"$set": {"status": "processed"}}
);
printf("Modified %d documents\n", result.modifiedCount);
hash<auto> result = coll.updateMany(
{"category": "new"},
{"$set": {"reviewed": True}},
{"upsert": True}
);
- Exceptions
-
| MONGODB-COLLECTION-ERROR | update failed |
◆ updateOne()
| hash< auto > mongodb::MongoCollection::updateOne |
( |
hash< auto > |
filter, |
|
|
hash< auto > |
update, |
|
|
*hash< auto > |
options |
|
) |
| |
Updates a single document in the collection.
- Parameters
-
| filter | the query filter to select the document |
| update | the update operations (use $set, $inc, etc.) |
| options | optional update options (e.g., upsert: True) |
- Returns
- a hash containing
matchedCount, modifiedCount, and optionally upsertedId
- Example:
hash<auto> result = coll.updateOne(
{"_id": oid},
{"$set": {"status": "active"}, "$inc": {"version": 1}}
);
printf("Modified: %d\n", result.modifiedCount);
hash<auto> result = coll.updateOne(
{"name": "test"},
{"$set": {"value": 1}},
{"upsert": True}
);
if (result.upsertedId) {
printf("Inserted new document with id: %y\n", result.upsertedId);
}
- Exceptions
-
| MONGODB-COLLECTION-ERROR | update failed |
The documentation for this class was generated from the following file: