Explicit and Implicit Syntax


  • You can declare your data nodes and edges explicitly, or let RavenDB do it for you by simply including nodes and edges in your search pattern as if they have already been declared.
    Using explicit or implicit syntax is a readability preference with no performance implications.
  • The explicit syntax may be preferable when you need to express non-trivial logic.
  • The implicit syntax may be preferable when you want to be simple or more concise.
  • Querying indexes is currently available only when you use the explicit syntax.

Sample queries included in this article use only data that is available in the Northwind sample database, so you may easily try them out.


Implicit and Explicit Queries

Here is a simple query that retrieves all documents from the Orders collection.

//Declare a data node
with {from Orders} as orders
    
//Create the search pattern
match 
    (orders)

It uses an explicit syntax: nodes and edges are explicitly declared using a with clause before including them in the search pattern.

with clauses allow you to define arbitrary restrictions on matches for selected data nodes or edges.

And here is a second query, exactly equivalent to the first, that uses an implicit syntax.

//Create the search pattern
match
    (Orders as orders)

Implicit queries allow you to include nodes and edges in your search pattern without explicitly declaring them first, inviting RavenDB to declare them automatically.

The two queries produce the exact same results:
Retrieve All Orders


Explicitly Declaring Data Elements

  • Declaring Data Nodes
    Declare a data node using a with clause with curly brackets.
    Provide each node with a unique alias.

    The following query retrieves all documents from the Products collection.

    //Declare a data node
    with {from Products} as products
        
    match 
        (products)
    Data Nodes: Products

    Data Nodes: Products

  • Declaring Edges
    Declare an edge using a with edges clause with parentheses.
    Provide each edge with a unique alias.

    The following query shows the relations between products and their suppliers.

    //Declare data nodes
    with {from Products} as products
    with {from Suppliers} as suppliers
    
    //Declare an edge
    with edges (Supplier) as supplier
    
    match 
         (products)-
         [supplier]->
         (suppliers)
    Documents are retrieved from the Products collection, and each product is related to its supplier's profile by the edge: an ID string in the product's Supplier property.
    Edges between Products and Suppliers

    Edges between Products and Suppliers


Mixing Explicit and Implicit Declarations

You can freely mix and match the explicit and implicit syntaxes.
E.g. -

//Declare a data node
with {from Products} as products
    
//Create the search pattern with declared and undeclared elements
match 
     (products)-
     [Supplier as supplier]->
     (Suppliers as suppliers)