Represents a collection of documents in a schema.
A collection object can be obtained from Schema::getCollection()
method:
Schema db;
Collection myColl;
myColl= db.getCollection("My Collection");
or directly constructed as follows:
Schema db;
Collection myColl(db, "My Collection");
When creating a Collection
object, by default no checks are made that it actually exists in the database. An operation that is executed on the server and involves such a non-existent collection throws an error. Call existsInDatabase()
to check the existence of the collection.
- Note
- A
Collection
object should be used by at most one thread at a time. It is not safe to call its methods by several threads simultaneously. It is responsibility of the user to ensure this using a synchronization mechanism such as mutexes.
Replace the document with the given id by a new one.
Specify the new document as either a DbDoc
object, a JSON string or an expr(docexpr)
argument, where docexpr
is like a JSON string but field values are given by expressions to be evaluated on the server.
If a document with the given id does not exist in the collection, nothing is done and the returned Result
object indicates that no documents were modified.
- Note
- If expressions are used, they can not use named parameters because it is not possible to bind values prior to execution of
replaceOne()
operation.
Add a new document or replace an existing document with the given id.
Specify the new document as either a DbDoc
object, a JSON string or an expr(docexpr)
argument, where docexpr
is like a JSON string but field values are given by expressions to be evaluated on the server.
If a document with the given id does not exist, the new document is added to the collection.
- Note
- If expressions are used, they can not use named parameters because it is not possible to bind values prior to execution of
addOrReplaceOne()
operation.