The MongoCursor class provides iteration over MongoDB query results.
More...
#include <QC_MongoCursor.dox.h>
|
| nothing | close () |
| | Closes the cursor and releases resources.
|
| |
| bool | more () |
| | Checks if there are more documents available.
|
| |
| *hash< auto > | next () |
| | Returns the next document from the cursor.
|
| |
| list< hash< auto > > | toList () |
| | Returns all remaining documents as a list.
|
| |
The MongoCursor class provides iteration over MongoDB query results.
This class is returned by find() and aggregate() operations and allows iterating over the result documents one at a time.
- Example:
MongoCursor cursor = coll.find({"status": "active"});
while (*hash<auto> doc = cursor.next()) {
printf("Document: %y\n", doc);
}
MongoCursor cursor = coll.find();
list<hash<auto>> docs = cursor.toList();
- Since
- mongodb 1.0
◆ close()
| nothing mongodb::MongoCursor::close |
( |
| ) |
|
Closes the cursor and releases resources.
- Example:
- Note
- Cursors are automatically closed when garbage collected, but calling close() explicitly is recommended for long-lived cursor objects.
◆ more()
| bool mongodb::MongoCursor::more |
( |
| ) |
|
Checks if there are more documents available.
- Returns
- True if there are more documents
- Example:
while (cursor.more()) {
hash<auto> doc = cursor.next();
// process doc
}
- Note
- This method may not be 100% accurate for all cursor types. It's generally better to use next() and check for nothing.
◆ next()
| *hash< auto > mongodb::MongoCursor::next |
( |
| ) |
|
Returns the next document from the cursor.
- Returns
- the next document, or nothing if no more documents
- Example:
while (*hash<auto> doc = cursor.next()) {
printf("Document: %y\n", doc);
}
- Exceptions
-
| MONGODB-CURSOR-ERROR | cursor error |
◆ toList()
| list< hash< auto > > mongodb::MongoCursor::toList |
( |
| ) |
|
Returns all remaining documents as a list.
- Returns
- a list of all remaining documents
- Example:
list<hash<auto>> docs = cursor.toList();
foreach hash<auto> doc in (docs) {
printf("Document: %y\n", doc);
}
- Exceptions
-
| MONGODB-CURSOR-ERROR | cursor error |
- Note
- This method exhausts the cursor. After calling toList(), subsequent calls to next() will return nothing.
The documentation for this class was generated from the following file: