Qore mongodb Module 2.3.0
Loading...
Searching...
No Matches
mongodb::MongoCollection Class Reference

The MongoCollection class provides MongoDB collection operations. More...

#include <QC_MongoCollection.dox.h>

Public Member Methods

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.
 

Detailed Description

The MongoCollection class provides MongoDB collection operations.

This class represents a MongoDB collection and provides methods for CRUD operations, aggregation, and collection management.

Example:
# Get a collection
MongoCollection users = db.getCollection("users");
# Insert a document
hash<auto> result = users.insertOne({"name": "John", "age": 30});
# Find documents
MongoCursor cursor = users.find({"age": {"$gt": 25}});
while (*hash<auto> doc = cursor.next()) {
printf("Found: %y\n", doc);
}
# Update documents
users.updateOne({"name": "John"}, {"$set": {"age": 31}});
# Delete documents
users.deleteMany({"age": {"$lt": 18}});
Since
mongodb 1.0

Member Function Documentation

◆ aggregate()

MongoCursor mongodb::MongoCollection::aggregate ( list< hash< auto > >  pipeline)

Runs an aggregation pipeline on the collection.

Parameters
pipelinea 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-ERRORaggregation failed

◆ countDocuments()

int mongodb::MongoCollection::countDocuments ( *hash< auto >  filter)

Counts the number of documents matching a filter.

Parameters
filterthe 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-ERRORcount failed

◆ deleteMany()

hash< auto > mongodb::MongoCollection::deleteMany ( hash< auto >  filter)

Deletes multiple documents from the collection.

Parameters
filterthe 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-ERRORdelete failed

◆ deleteOne()

hash< auto > mongodb::MongoCollection::deleteOne ( hash< auto >  filter)

Deletes a single document from the collection.

Parameters
filterthe 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-ERRORdelete 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-ERRORdrop failed

◆ find()

MongoCursor mongodb::MongoCollection::find ( *hash< auto >  filter,
*hash< auto >  opts 
)

Finds documents in the collection.

Parameters
filterthe query filter (optional, empty hash matches all)
optsfind options (optional): projection, sort, limit, skip
Returns
a MongoCursor for iterating the results
Example:
# Find all documents
MongoCursor cursor = coll.find();
# Find with filter
MongoCursor cursor = coll.find({"status": "active"});
# Find with options
MongoCursor cursor = coll.find({"status": "active"}, {
"projection": {"name": 1, "age": 1},
"sort": {"age": -1},
"limit": 10
});
Exceptions
MONGODB-COLLECTION-ERRORfind failed

◆ findOne()

*hash< auto > mongodb::MongoCollection::findOne ( *hash< auto >  filter)

Finds a single document in the collection.

Parameters
filterthe query filter
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-ERRORfind 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
documentsa 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-ERRORinsert failed

◆ insertOne()

hash< auto > mongodb::MongoCollection::insertOne ( hash< auto >  document)

Inserts a single document into the collection.

Parameters
documentthe 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-ERRORinsert failed

◆ replaceOne()

hash< auto > mongodb::MongoCollection::replaceOne ( hash< auto >  filter,
hash< auto >  replacement,
*hash< auto >  options 
)

Replaces a single document in the collection.

Parameters
filterthe query filter to select the document
replacementthe replacement document (must not contain update operators)
optionsoptional 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);
# With upsert
hash<auto> result = coll.replaceOne(
{"name": "test"},
{"name": "test", "value": 1, "created": now()},
{"upsert": True}
);
Exceptions
MONGODB-COLLECTION-ERRORreplace failed

◆ updateMany()

hash< auto > mongodb::MongoCollection::updateMany ( hash< auto >  filter,
hash< auto >  update,
*hash< auto >  options 
)

Updates multiple documents in the collection.

Parameters
filterthe query filter to select documents
updatethe update operations
optionsoptional 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);
# With upsert
hash<auto> result = coll.updateMany(
{"category": "new"},
{"$set": {"reviewed": True}},
{"upsert": True}
);
Exceptions
MONGODB-COLLECTION-ERRORupdate failed

◆ updateOne()

hash< auto > mongodb::MongoCollection::updateOne ( hash< auto >  filter,
hash< auto >  update,
*hash< auto >  options 
)

Updates a single document in the collection.

Parameters
filterthe query filter to select the document
updatethe update operations (use $set, $inc, etc.)
optionsoptional 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);
# With upsert
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-ERRORupdate failed

The documentation for this class was generated from the following file: