pub trait View: Sized + Send + Sync + Debug + 'static {
    type Collection: Collection;
    type Key: for<'a> Key<'a> + PartialEq + 'static;
    type Value: Send + Sync;

    fn name(&self) -> Name;

    fn view_name(&self) -> ViewName { ... }
}
Expand description

An lazy index of mapped and/or reduced data from a Collection.

A view provides an efficient way to query data within a collection. BonsaiDb indexes the associated View::Collection by calling CollectionViewSchema::map()/ViewSchema::map() every time a document is created or updated. The result Mappings form a sorted index that can be efficiently queried using the View::Key type.

A View behaves similarly to the standard library’s BTreeMap with these types: BTreeMap<View::Key, Vec<(Header, View::Value)>>

For a deeper dive on Views, see the section in our user’s guide.

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 collection this view belongs to

The key for this view.

An associated type that can be stored with each entry in the view.

Required Methods

The name of the view. Must be unique per collection.

Provided Methods

The namespaced name of the view.

Implementors