MapReduce Views Using the .NET SDK with Couchbase Server

You can use MapReduce views to create queryable indexes in Couchbase Data Platform.

The normal CRUD methods allow you to look up a document by its ID. A MapReduce (view query) allows you to lookup one or more documents based on various criteria. MapReduce views are comprised of a map function that is executed once per document (this is done incrementally, so this is not run each time you query the view) and an optional reduce function that performs aggregation on the results of the map function. The map and reduce functions are stored on the server and written in JavaScript.

MapReduce queries can be further customized during query time to allow only a subset (or range) of the data to be returned.

See the Incremental MapReduce Views and Querying Data with Views sections of the general documentation to learn more about views and their architecture.

By Name Views

The following example is the definition of a by_name view in a "beer" design document. This view checks whether a document is a beer and has a name. If it does, it emits the beer’s name into the index. This view allows beers to be queried for by name. For example, it’s now possible to ask the question "What beers start with A?"

var result = bucket.ViewQuery<Beer>("beers", "by_name", options => {
    options.WithStartKey("A");
    options.WithLimit(10);
});

The following example is the definition of a by_name view in a "landmarks" design document in the "travel-sample" sample dataset. This view checks whether a document is a landmark and has a name. If it does, it emits the landmark’s name into the index. This view allows landmarks to be queried for by its "name" field.

var result = bucket.ViewQuery<Landmark>("landmarks", "by_name", options => {
    options.WithKey("<landmark-name>");
});

A Spatial View can instead be queried with a range or bounding box. For example, let’s imagine we have stored landmarks with coordinates for their home city (eg. Paris, Vienna, Berlin and New York) under geo, and each city’s coordinates is represented as two attributes, lon and lat. The following spatial view map function could be used to find landmarks within Europe, as a "by_location" view in a "spatial" design document:

(10) );

foreach (var row in result.Rows) { var id = row.Id; var key = row.Key; var value = row.Value; }

// We only need the briefest of samples - the content brought in at the top of the page will recommend avoiding Views.[source,javascript]

function (doc, meta) { if (doc.type && doc.type == "landmark" && doc.geo) { emit([doc.geo.lon, doc.geo.lat], null); } }

== Querying Views through the .NET SDK




Once you have a view defined, it can be queried from the .NET SDK by using the `ViewQuery` method on a Bucket instance.

Here is an example:

[source,csharp]

var result = bucket.ViewQuery<Type>(“design-doc”, “view-name”, options ⇒ options.WithLimit

  • Those partial files include **

docs-sdk-common/modules/shared/partials/views.adoc

MapReduce Views

You can use MapReduce views to create queryable indexes in Couchbase Server.
Although still maintained and supported for legacy use, Views date from the earliest days of Couchbase Server development, and as such are rarely the best choice over, say, xref:[our Query service] for your application, see dotnet-sdk:concept-docs:http-services.adoc.

The normal CRUD methods allow you to look up a document by its ID. A MapReduce (view query) allows you to lookup one or more documents based on various criteria. MapReduce views are comprised of a map function that is executed once per document (this is done incrementally, so this is not run each time you query the view) and an optional reduce function that performs aggregation on the results of the map function. The map and reduce functions are stored on the server and written in JavaScript.

MapReduce queries can be further customized during query time to allow only a subset (or range) of the data to be returned.

See the Incremental MapReduce Views and Querying Data with Views sections of the general documentation to learn more about views and their architecture.

By Name Views

The following example is the definition of a by_name view in a "beer" design document. This view checks whether a document is a beer and has a name. If it does, it emits the beer’s name into the index. This view allows beers to be queried for by name. For example, it’s now possible to ask the question "What beers start with A?"

The following example is the definition of a by_name view in a "landmarks" design document in the "travel-sample" sample dataset. This view checks whether a document is a landmark and has a name. If it does, it emits the landmark’s name into the index. This view allows landmarks to be queried for by its "name" field.

A Spatial View can instead be queried with a range or bounding box. For example, let’s imagine we have stored landmarks with coordinates for their home city (eg. Paris, Vienna, Berlin and New York) under geo, and each city’s coordinates is represented as two attributes, lon and lat. The following spatial view map function could be used to find landmarks within Europe, as a "by_location" view in a "spatial" design document: