What is a Document Query?



DocumentQuery examples

Query collection - no filtering

// Query for all documents from 'Employees' collection
List<Employee> allEmployees = session.Advanced
    .DocumentQuery<Employee>()
    .ToList();
// Query for all documents from 'Employees' collection
List<Employee> allEmployees = await asyncSession.Advanced
    .AsyncDocumentQuery<Employee>() 
    .ToListAsync();  
from "Employees"

Query collection - by ID

// Query collection by document ID
Employee employee = session.Advanced
    .DocumentQuery<Employee>()
    .WhereEquals(x => x.Id, "employees/1-A")
    .FirstOrDefault();
// Query collection by document ID
Employee employee = await asyncSession.Advanced
    .AsyncDocumentQuery<Employee>()
    .WhereEquals(x => x.Id, "employees/1-A")
    .FirstOrDefaultAsync();  
from "Employees" where id() == "employees/1-A"

Query collection - with filtering

// Query collection - filter by document field
List<Employee> employees = session.Advanced
    .DocumentQuery<Employee>()
    .WhereEquals(x => x.FirstName, "Robert")
    .ToList();
// Query collection - filter by document field
List<Employee> employees = await asyncSession.Advanced
    .AsyncDocumentQuery<Employee>()
    .WhereEquals(x => x.FirstName, "Robert")
    .ToListAsync();  
from "Employees" where FirstName == "Robert"

Query collection - with paging

// Query collection - page results
List<Product> products = session.Advanced
    .DocumentQuery<Product>()
    .Skip(5)   // Skip first 5 results
    .Take(10)  // Load up to 10 entities from 'Products' collection
    .ToList();
// Query collection - page results
List<Product> products = await asyncSession.Advanced
    .AsyncDocumentQuery<Product>()
    .Skip(5)   // Skip first 5 results
    .Take(10)  // Load up to 10 entities from 'Products' collection
    .ToListAsync();  
from "Products" limit 5, 10 // skip 5, take 10

Query an index

Please refer to Querying an index for examples of querying an index using a DocumentQuery.

Convert between DocumentQuery and Query

DocumentQuery to Query

A DocumentQuery can be converted to a Query.
This enables you to take advantage of all available LINQ extensions provided by RavenDB.

// Define a DocumentQuery
var docQuery = session.Advanced
    .DocumentQuery<Order>(); // 'IDocumentQuery' instance

// Convert to Query
var query = docQuery.ToQueryable(); // 'IRavenQueryable' instance

// Apply any 'IRavenQueryable' LINQ extension
var queryResults = query
    .Where(x => x.Freight > 25)
    .ToList();
// Define a DocumentQuery
var docQuery = asyncSession.Advanced
    .AsyncDocumentQuery<Order>(); // 'IAsyncDocumentQuery' instance

// Convert to Query
var query = docQuery.ToQueryable(); // 'IRavenQueryable' instance

// Apply any 'IRavenQueryable' LINQ extension
var queryResults = await query
    .Where(x => x.Freight > 25) 
    .ToListAsync();
from "Orders" where Freight > 25

Convert DocumentQuery to Query when you need to project data from a related document
in a dynamic query.

// Define a DocumentQuery
var docQuery = session.Advanced
    .DocumentQuery<Order>()
    .WhereGreaterThan("Freight", 25);

// Convert to Query
var query = docQuery.ToQueryable();

// Define the projection on the query using LINQ
var projectedQuery = from order in query
    // Load the related document
    let company = session.Load<Company>(order.Company)
    // Define the projection
    select new
    {
        Freight = order.Freight,   // data from the Order document
        CompanyName = company.Name // data from the related Company document
    };

// Execute the query
var queryResults = projectedQuery.ToList();
from "Orders" as o
where o.Freight > 25
load o.Company as c
select {
    Freight: o.Freight,
    CompanyName: c.Name
}

Query to DocumentQuery

A Query can be converted to a DocumentQuery.
This enables you to take advantage of the API available only for DocumentQuery.

// Define a Query
var query = session
    .Query<Order>()
    .Where(x => x.Freight > 25);
    
// Convert to DocumentQuery
var docQuery = query.ToDocumentQuery();

// Apply a DocumentQuery method (e.g. IncludeExplanations is Not available on Query)
docQuery.IncludeExplanations(out Explanations exp);

// Execute the query
var docQueryResults = docQuery.ToList();
// Define a Query
var query = asyncSession
    .Query<Order>()
    .Where(x => x.Freight > 25);
    
// Convert to DocumentQuery
var docQuery = query.ToAsyncDocumentQuery();

// Apply a DocumentQuery method (e.g. IncludeExplanations is Not available on Query)
docQuery.IncludeExplanations(out Explanations exp);

// Execute the query
var docQueryResults = docQuery.ToListAsync();
from "Orders"
where Freight > 25
include explanations()

Custom Methods and Extensions

Several methods share the same functionality as their Query counterparts.
Refer to the corresponding documentation articles, marked with links starting with "[Query]" in the list below.

Available custom methods and extensions:

Syntax

The available DocumentQuery overloads are listed in this Syntax section in the Query Overview.