Keys in Spatial View Functions

The key must be a multidimensional bounding box.

The keys in a spatial view function can defined in several ways:

Single values

When a single value is used in the key, it expands to a collapsed range.

For example, the [1, 2] value is stored as [[1, 1], [2, 2]].

Ranges

Use ranges for keys, for example, for shops with certain opening hours such as 10:00 to 20:00. This example uses timestamps and converts the values of 10:00 - 20:00 to 1000 and 2000.

emit([[1000, 2000]], null);

GeoJSON geometry

You can use a GeoJSON geometry in a key. It must be the first element of the array. The bounding box is automatically calculated and used as the range.

Couchbase Server supports the following GeoJSON objects:

  • Point

  • MultiPoint

  • LineString

  • MultiLineString

  • Polygon

  • MultiPolygon

  • GeometryCollection

Examples

In this example, the key is stored internally as [[10.9, 10.9], [48.4, 48.4]].

 emit([{
   "type": "Point",
   "coordinates":[10.9, 48.4]
}], null);

In this example, the key is stored internally as [[10.9, 10.9], [48.4, 48.4]].

emit([{
   "type": "Point",
   "coordinates":[10.9, 48.4]
}, [1000, 2000], 5], null);
For backward compatibility, you can also emit a GeoJSON geometry that is not wrapped in an array.

The value can be any arbitrary valid JSON. Here is an example spatial view function that emits a point with its height:

function(doc, meta) {
  if (doc.loc && doc.height && doc.name) {
     emit([doc.loc.lon, doc.loc.lat, doc.height], doc.name);
  }
}

You can put a spatial view function into a design document by using the object name spatial to indicate the nature of the view definition. For example, the following design document includes the previous function as a view named points.

  {
   "spatial" : {
      "points" : "function(doc, meta) {if (doc.loc  && doc.height && doc.name) {
          emit([doc.loc.lon, doc.loc.lat, doc.height], doc.name);}}"
   }
}

If your document already contains GeoJSON encoded data, you can directly emit that geometry:

function(doc, meta) {
  if (doc.geometry && doc.name) {
     emit([doc.geometry], doc.name);
  }
}

To execute the spatial query, use the design document format that uses the embedded spatial function. For example, a design document named main that resides within the bucket places is executed with this URL:

http://[localhost]:8092/places/_design/main/_spatial/points.