Skip to main content

Commands: Querying: How to query a database?

Use Query method to fetch results of a selected index according to a specified query.

Syntax

QueryResult Query(
string index,
IndexQuery query,
string[] includes = null,
bool metadataOnly = false,
bool indexEntriesOnly = false);
Parameters
indexstringA name of an index to query
queryIndexQueryA query definition containing all information required to query a specified index.
includesstring[]An array of relative paths that specify related documents ids which should be included in a query result.
metadataOnlyboolTrue if returned documents should include only metadata without a document body.
indexEntriesOnlyboolTrue if query results should contain only index entries.
Return Value
QueryResultObject which represents results of a specified query.

Example I

A sample Query method call that returns orders for a company specified:

result = store.DatabaseCommands.Query("Orders/Totals", new IndexQuery
{
Query = "Company:companies/1"
});

List<RavenJObject> users = result.Results; // documents resulting from this query - orders

Example II

If a model of your documents is such that they reference others and you want to retrieve them together in a single query request, then you need to specify paths to properties that contain IDs of referenced documents:

result = store.DatabaseCommands.Query("Orders/Totals", new IndexQuery(),
new[]
{
"Company",
"Employee"
});

List<RavenJObject> referencedDocs = result.Includes; // included documents - companies and employees