An lazy index of mapped and/or reduced data from a
A view provides an efficient way to query data within a collection. BonsaiDb
indexes the associated
View::Collection by calling
ViewSchema::map() every time a document
is created or updated. The result
Mappings form a sorted index that can
be efficiently queried using the
A View behaves similarly to the standard library’s BTreeMap with these
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
an efficient manner.
ViewSchema::map() function is responsible for “mapping” data from the
stored document into the
Key type. In the example below, the field
being used as the View’s
Key type, and the field
score is being
used as the View’s
This enables querying the view in many powerful ways:
db.view::<MyView>().query().await: Return all entries in the view (without the associated documents).
db.view::<MyView>().query_with_docs().await: Return all entries in the view, including the associated
db.view::<MyView>().query_with_collection_docs().await: Return all entries in the view, including the associated
db.view::<MyView>().reduce().await: Returns the reduced value of the view query. For the example below, the result type of this call is
db.view::<MyView>().reduce_grouped().await: Returns the reduced value of the view query, grouped by key. For the example below, the
valuereturned will be an
f32that is the average
scoreof all documents with a matching
All of the queries above can be filtered and customized by the methods available
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.
The name of the view. Must be unique per collection.
The namespaced name of the view.