Collection
A Collection
is a container for documents similar to a table in a relational database. Collections are grouped into namespaces called scope
s and have a unique name within that scope.
Every database contains a default collection named "_default" (the constant Collection.DEFAULT_NAME
) in the default scope (also named "_default", the constant Scope.DEFAULT_NAME
)
While creating a new collection requires the name of the collection and the name of its scope, there are convenience methods that take only the name of the collection and create the collection in the default scope.
The naming rules for collections and scopes are as follows:
Must be between 1 and 251 characters in length.
Can only contain the characters A-Z, a-z, 0-9, and the symbols _, -, and %.
Cannot start with _ or %.
Both scope and collection names are case-sensitive.
Collection
objects are only valid during the time the database that contains them is open. An attempt to use a collection that belongs to a closed database will throw an IllegalStateException
. An application can hold references to multiple instances of a single database. Under these circumstances, it is possible that a collection will be deleted in one instance before an attempt to use it in another. Such an attempt will also cause an IllegalStateException
to be thrown.
Collection
s are AutoCloseable
. While garbage collection will manage them correctly, developers are strongly encouraged to use them in try-with-resources blocks or to close them explicitly, after use.
Types
Functions
Add a change listener to listen to change events occurring to any documents in the collection. To remove the listener, call remove() function on the returned listener token.
Add a change listener to listen to change events occurring to any documents in the collection.
Add a change listener to listen to change events occurring to a document of the given document id. To remove the listener, call remove() function on the returned listener token.
Add a change listener to listen to change events occurring to a document of the given document id.
A Flow of Collection changes.
Add an index to the collection.
Delete a document from the collection. The default concurrency control, lastWriteWins, will be used when there is conflict during delete. If the document doesn't exist in the collection, the NotFound error will be thrown.
Delete a document from the collection with a specified concurrency control. When specifying the failOnConflict concurrency control, and conflict occurred, the delete operation will fail with 'false' value returned.
Delete the named index from the collection.
A Flow of document changes
Gets document fragment object by the given document ID.
Gets an existing Document object with the given ID. If the document with the given ID doesn't exist in the collection, the value returned will be null.
Get the expiration date set to the document of the given id.
When purging a document, the collection instance of the document and this collection instance must be the same, otherwise, the InvalidParameter error will be thrown.
Purge a document by id from the collection. If the document doesn't exist in the collection, the NotFound error will be thrown.
Save a document into the collection. The default concurrency control, lastWriteWins, will be used when there is conflict during save.
Save a document into the collection with a specified concurrency control. When specifying the failOnConflict concurrency control, and conflict occurred, the save operation will fail with 'false' value returned. When saving a document that already belongs to a collection, the collection instance of the document and this collection instance must be the same, otherwise, the InvalidParameter error will be thrown.
Save a document into the collection with a specified conflict handler. The specified conflict handler will be called if there is conflict during save. If the conflict handler returns 'false', the save operation will be canceled with 'false' value returned.
Set an expiration date to the document of the given id. Setting a nil date will clear the expiration.