pub trait ViewSchema: Send + Sync + Debug + 'static {
    type View: SerializedView;

    fn map(&self, document: &BorrowedDocument<'_>) -> ViewMapResult<Self::View>;

    fn unique(&self) -> bool { ... }
    fn lazy(&self) -> bool { ... }
    fn version(&self) -> u64 { ... }
    fn reduce(
        mappings: &[ViewMappedValue<Self::View>],
        rereduce: bool
    ) -> Result<<Self::View as View>::Value, Error> { ... } }
Expand description

The implementation of Map/Reduce for a View.

Views allow querying documents contained within Collections in an efficient manner.

The ViewSchema::map() function is responsible for “mapping” data from the stored document into the Key type. In the example below, the field rank is being used as the View’s Key type, and the field score is being used as the View’s Value

The ViewSchema::reduce() function is responsible for “reducing” a list of Values into a single value. The example below implements reduce() to return an average value.

This enables querying the view in many powerful ways:

All of the queries above can be filtered and customized by the methods available on connection::View.

For a more detailed walkthrough, see our user guide’s section on Views.

Required Associated Types§

The view this schema is defined for.

Required Methods§

The map function for this view. This function is responsible for emitting entries for any documents that should be contained in this View. If None is returned, the View will not include the document. See the user guide’s chapter on views for more information on how map works.

Provided Methods§

If true, no two documents may emit the same key. Unique views are updated when the document is saved, allowing for this check to be done atomically. When a document is updated, all unique views will be updated, and if any of them fail, the document will not be allowed to update and an Error::UniqueKeyViolation will be returned.

Returns whether this view should be lazily updated. If true, views will be updated only when accessed. If false, views will be updated during the transaction that is updating the affected documents.

The version of the view. Changing this value will cause indexes to be rebuilt.

Returns a value that is produced by reducing a list of mappings into a single value. If rereduce is true, the values contained in the mappings have already been reduced at least one time. If an error of ReduceUnimplemented is returned, queries that ask for a reduce operation will return an error. See the user guide’s chapter on views for more information on how reduce works.