Skip to main content

Session: Querying: What is a Document Query?

Low-level querying capabilities can be accessed via the DocumentQuery method in advanced session operations. DocumentQuery gives you more flexibility and control over the process of building a query.

Syntax

IDocumentQuery<T> DocumentQuery<T, TIndexCreator>();

IDocumentQuery<T> DocumentQuery<T>(
string indexName = null,
string collectionName = null,
bool isMapReduce = false);
Parameters
indexNamestringName of an index to perform a query on (exclusive with collectionName)
collectionNamestringName of a collection to perform a query on (exclusive with indexName)
isMapReduceboolIndicates if a queried index is a map/reduce index (modifies how we treat identifier properties)
Return Value
IDocumentQueryInstance implementing IDocumentQuery interface containing additional query methods and extensions

Example I - Basic

// load all entities from 'Employees' collection
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee>()
.ToList();
// load all entities from 'Employees' collection
// where FirstName equals 'Robert'
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee>()
.WhereEquals(x => x.FirstName, "Robert")
.ToList();

Example II - Querying Specified Index

// load all entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee>("My/Custom/Index")
.WhereEquals(x => x.FirstName, "Robert")
.ToList();

or

// load all entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee, MyCustomIndex>()
.WhereEquals("FirstName", "Robert")
.ToList();

ToQueryable

To convert DocumentQuery to Query and using LINQ to project you only need to use ToQueryable method.

Example - Converting DocumentQuery to Query and using LINQ to project

// load all entities from 'Order' collection
var dq = session.Advanced.DocumentQuery<Order>()
.WhereGreaterThan("Freight", 8)
.ToQueryable();

var q = from order in dq
let company = session.Load<Company>(order.Company)
select new
{
order.Freight,
company.Name

};

var results = q.ToList();

See Projections for more information.

Custom Methods and Extensions

Functionality of most of the methods match the functionality of their Query counterparts and therefore will not be described again. Please refer to the appropriate counterpart documentation articles. Links starting with [Query] are marking those articles.

Available custom methods and extensions:

Remarks

By default, if the page size is not specified, all of the matching records will be retrieved from a database.