Skip to main content

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 collection - by ID

// Query collection by document ID
Employee employee = session.Advanced
.DocumentQuery<Employee>()
.WhereEquals(x => x.Id, "employees/1-A")
.FirstOrDefault();

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 - 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 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();
  • 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();

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();

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.