11. Views
A view is a named read query kept in the _views collection, one document per version. Views are how a data
model is written in OGML: each view reads collections or other views, and queries, reports and other views read it like
a collection.
define view sales.paid
| description "Paid orders with their total"
| exec
| on orders | where status == "paid" | derive total = price * qty
| end
| end
{"name":"sales.paid","version":1,"state":"active","description":"Paid orders with their total",
"query":"on orders | where status == \"paid\" | derive total = price * qty",
"parameters":[],"reads":["orders"],"materialized":false,"createdBy":"…","createdAt":…}
11.1 define view
Definition. Writes a new version of a view.
Syntax
define view <name>
[| description "<text>"]
[| materialized]
| exec
| <query>
| end
| end
define view <name> | description "<text>" | end -- a new version, another description
define view <name> | version <n> | end -- restores version n as a new version
define view <name> | end -- a new version, identical
Written without | (3.2), the view of the chapter opening reads:
define view sales.paid
description "Paid orders with their total"
exec
on orders
where status == "paid"
derive total = price * qty
end
end
Its query keeps that text, line breaks included.
Behavior
<name>is identifiers joined by dots:ledger.cube,sales.by_month.execis the last clause; the body runs fromexecto the two finalend(the body’s ownendstay in it).- Each
definewrites a new version and marks the previous active onesuperseded. Withoutdescription, the description is kept; withoutmaterialized, a restore or a copy keeps the flag of the version it copies. - The body is checked when defined: planned with its parameters read as
1, and refused when it writes (view.invalid: a view only reads: its query cannot insert, set, delete or load), when it reads a view that does not exist, or when it makes a cycle. - The definition records:
| Field | Meaning |
|---|---|
name, version, state | state is active, superseded or deleted |
description | Free text |
query | The body, as written |
parameters | The $name the body reads without declaring them with let |
reads | The collections it reads, through the views it reads |
materialized | Whether its rows are kept (chapter 12) |
createdBy, createdAt | Author and time |
_place | The Place, for a Place view (absent for a standard view) |
- History is never rewritten: every version stays in
_views.
Who may define views. In a Place, its Owner. Outside a Place, a standard view, which needs the right to write
_views. explain define view … checks without writing.
Errors and limits
| Error | Cause |
|---|---|
view … is not defined | The body reads an unknown view |
cycle | The view reads itself, directly or through others |
a view only reads | The body writes |
a materialized view takes no parameter, its body binds them with let | A materialized body with a free $name |
cannot depend on documents | A let reads document fields |
| depth | Views read views 8 deep at most |
11.2 undefine view
undefine view <name>
Marks the active version deleted. The standard view of the same name, if any, is read again by the Place. The
cache of a materialized view leaves with it.
11.3 Reading a view
on view <name> [version <n>] [as <alias>]
lookup view <name> [version <n>] as <alias> | … | end
lookup view <name> as <alias> on <joined field> == <outer field> into <field>
union | on view <name> | … | end
let $rows = (on view <name> | …)
Behavior
- The view’s query replaces the source before the query is planned: the plan,
explainand authorization see the collections it reads, and the stages that follow are optimized with it (a filter after a view reaches the view’s source, 14.2). - In a Place, the Place’s view is read, else the standard view of that name; its active version, or
version <n>. lookup viewneeds a view whose source is a bareon <collection>oron values […];as <alias>names the view’s source, which must not have one already.- Views are resolved by the query service (the
query.executeoperation), not by the bare pipeline layer of the engine library.
Examples
on view sales.paid | group | by region | sum total | end
on view ledger.cube version 1 | union | on view ledger.cube | end
on orders | lookup view catalog.products as c on c.code == product into product
11.4 Parameters
A view’s parameters are the $name its body reads without declaring. They are bound by the reading query:
request params, or its let variables.
define view sales.since | exec | on orders | where month >= $since | end | end
let $since = "2026-02" | on view sales.since
let $name ?= <default>in the body gives a default, used when the reader binds nothing:
define view orders.big | exec | let $min ?= 2 | on orders | where qty >= $min | end | end
on view orders.big | count -- {"count":4}
let $min = 4 | on view orders.big | count -- {"count":2}
- The view’s own
letstay local: they may shadow a variable of the reading query, which cannot read them. - An unbound parameter fails the reading query:
$scenario is not bound. - A materialized view takes no parameter: its body binds them with
let.
11.5 Listing views
on _views | where state == "active" | select name, version, parameters, reads, materialized
on _views | where name == "sales.paid" | sort version | select version, state, createdAt, query
In a Place, _views shows only that Place’s views and the standard ones; only define and undefine write it.
11.6 Standard views
A view defined outside any Place is a standard view: every Place reads it, unless the Place defines a view of the same name, which then takes its place for that Place. Standard views suit definitions shared by every Place (a calendar, a reference classification); Place views suit what one Place models for itself.
Never assume a standard view exists: a node may hold none. Read
on _viewsfirst, or define what you need.
11.7 Views and Places
| Place view | Standard view | |
|---|---|---|
| Defined by | The Place Owner, in the Place | A principal allowed to write _views, outside any Place |
| Read by | That Place | Every Place without a view of that name |
| Stored with | _place = the Place | No _place |
| Rows | Of the reading Place | Of the reading Place |
A view never reads across Places: whichever view is read, its sources read the rows of the Place running the query.