Interface NitriteCollection

  • All Superinterfaces:
    org.dizitart.no2.common.meta.AttributesAware, AutoCloseable, EventAware, PersistentCollection<Document>

    public interface NitriteCollection
    extends PersistentCollection<Document>
    Represents a named document collection stored in Nitrite database. It persists documents into the database. Each document is associated with a unique NitriteId in a collection.

    A nitrite collection supports indexing. Every nitrite collection is also observable. It means, any change in a collection can be listened back via registering a CollectionEventListener.

    Create a collection
     
     Nitrite db = Nitrite.builder()
             .openOrCreate("user", "password");
     
     NitriteCollection collection = db.getCollection("products");
     
    
    Since:
    1.0
    Author:
    Anindya Chatterjee
    See Also:
    EventAware, Document, NitriteId, CollectionEventListener, EventBus
    • Method Detail

      • insert

        default WriteResult insert​(Document document,
                                   Document... documents)
        Inserts documents into a collection. If the document contains a NitriteId in its _id field, then it will be used as a unique key to identify the document in the collection. If the document does not contain any NitriteId, then a new NitriteId will be generated and inserted into the document.

        If any of the field is already indexed in the collection, then after insertion the index will also be updated.

        NOTE: These operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Insert.

        Parameters:
        document - the document to insert
        documents - other documents to insert in a batch.
        Returns:
        the result of write operation.
        Throws:
        ValidationException - if document is null.
        InvalidIdException - if the _id value contains non-comparable type, i.e. type that does not implement Comparable.
        UniqueConstraintException - if the value of _id value clashes with the id of another document in the collection.
        UniqueConstraintException - if a field of the document is indexed, and it violates the unique constraint in the collection(if any).
        See Also:
        NitriteId, WriteResult
      • update

        default WriteResult update​(Filter filter,
                                   Document update)
        Update documents in the collection.

        If the filter is null, it will update all documents in the collection.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Update.

        Parameters:
        filter - the filter to apply to select documents from the collection.
        update - the modifications to apply.
        Returns:
        the result of the update operation.
        Throws:
        ValidationException - if the update document is null.
      • update

        WriteResult update​(Filter filter,
                           Document update,
                           UpdateOptions updateOptions)
        Updates document in the collection. Update operation can be customized with the help of updateOptions.

        If the filter is null, it will update all documents in the collection unless justOnce is set to true in updateOptions.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Update or EventType.Insert.

        Parameters:
        filter - the filter to apply to select documents from the collection.
        update - the modifications to apply.
        updateOptions - the update options to customize the operation.
        Returns:
        the result of the update operation.
        Throws:
        ValidationException - if the update document is null.
        ValidationException - if updateOptions is null.
        See Also:
        UpdateOptions
      • remove

        default WriteResult remove​(Filter filter)
        Removes matching elements from the collection.

        If the filter is null, it will remove all objects from the collection.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Remove.

        Parameters:
        filter - the filter to apply to select elements from collection.
        Returns:
        the result of the remove operation.
      • remove

        WriteResult remove​(Filter filter,
                           boolean justOne)
        Removes document from a collection. Remove operation can be customized by removeOptions.

        If the filter is null, it will remove all documents in the collection unless justOnce is set to true in removeOptions.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Remove.

        Parameters:
        filter - the filter to apply to select documents from collection.
        justOne - indicates if only one element will be removed or all of them.
        Returns:
        the result of the remove operation.
      • find

        default DocumentCursor find()
        Returns a cursor to all documents in the collection.
        Returns:
        a cursor to all documents in the collection.
      • find

        default DocumentCursor find​(Filter filter)
        Applies a filter on the collection and returns a cursor to the selected documents.

        See Filter for all available filters.

        NOTE: If there is an index on the value specified in the filter, this operation will take advantage of the index.

        Parameters:
        filter - the filter to apply to select documents from collection.
        Returns:
        a cursor to all selected documents.
        Throws:
        ValidationException - if filter is null.
        See Also:
        Filter, DocumentCursor.project(Document)
      • find

        default DocumentCursor find​(FindOptions findOptions)
        Returns a customized cursor to all documents in the collection.
        Parameters:
        findOptions - specifies pagination, sort options for the cursor.
        Returns:
        a cursor to all selected documents.
        See Also:
        SortOrder
      • find

        DocumentCursor find​(Filter filter,
                            FindOptions findOptions)
        Applies a filter on the collection and returns a customized cursor to the selected documents.

        NOTE: If there is an index on the value specified in the filter, this operation will take advantage of the index.

        Parameters:
        filter - the filter to apply to select documents from collection.
        findOptions - specifies pagination, sort options for the cursor.
        Returns:
        a cursor to all selected documents.
      • getById

        Document getById​(NitriteId nitriteId)
        Gets a single element from the collection by its id. If no element is found, it will return null.
        Parameters:
        nitriteId - the nitrite id
        Returns:
        the unique document associated with the nitrite id.
        Throws:
        ValidationException - if `nitriteId` is null.