Querying Data and Query Data Service
Couchbase Server provides different ways to query data using either document keys, view queries, spatial queries, or N1QL queries.
- Retrieving data with document key
The data service provides a key-value API that returns a document based on a given key. The API returns the full document with low latency. This method is restrictive as you can use this method only if you have the document keys in your hand. However, it is the fastest way to retrieve data. For more information on retrieving documents with document keys, see Data Service and Core Data Access. This retrieval method is available through the data service.
- Querying data using View queries
Views provide an API to query any existing map-reduce view. Views take in a user defined map and reduce function to process a JSON document and emit document attributes as the view keys. View queries can query on the view keys to retrieve the values defined by the map and reduce functions or the document keys. Views are typically useful for interactive reporting type queries where complex data processing and custom data reshaping is necessary. This query method is available through the data service. For more information, see Querying Data with Views.
- Querying data using Spatial queries
Spatial Views provide an API to query any existing spatial view. Spatial Views take in a user defined map function to process geographic information in JSON document and emit geographic coordinates as the view keys. Spatial View queries can query the geographic information using multidimensional bounding boxes that can retrieve the documents that have coordinates and are contained in the bounding box. Spatial Views are typically useful for location aware applications with geographic queries. This query method is available through the data service. For more information, see Querying Geographic Data with Spatial Views.
- Querying data using N1QL queries
N1QL provides a SQL like query language and is an expressive way to query data in JSON document. Unlike views, N1QL does not require a dedicated index and can issue ad-hoc queries on data. N1QL can also define indexes that can engage the GSI indexer or the View indexer. Indexes provide fast lookup for N1QL queries. N1QL queries are best for interactive applications that need low latency response times. This method is available through the query service. For more information, see Querying Data with N1QL.