cosmos-db
  1. cosmos-db-geospatial-queries

Geospatial Queries - (CosmosDB Advanced Topics)

CosmosDB is a scalable NoSQL database that supports a variety of data models, including documents, key-value pairs, graphs, and column-family. One of the advanced features of CosmosDB is support for geospatial queries, which allow you to perform queries based on geographic location. In this tutorial, we'll discuss how to use geospatial queries in CosmosDB.

Syntax

The syntax for geospatial queries in CosmosDB involves defining a stored procedure or a user-defined function that takes a location parameter. Here is an example of the syntax for a stored procedure that performs a geospatial query:

function getLocationsNear(latitude, longitude) {
    var collection = getContext().getCollection();
    var query = "SELECT VALUE c.name FROM c WHERE ST_DISTANCE(c.location, {'type': 'Point', 'coordinates':[" + longitude + ", " + latitude + "]}) < 1000";
    var isAccepted = collection.queryDocuments(collection.getSelfLink(), query,
        function (err, documents, responseOptions) {
            if (err) throw new Error("Error");
            getContext().getResponse().setBody(documents);
        });
    if (!isAccepted) throw new Error("The query was not accepted by the server.");
}

Example

Suppose you have a CosmosDB collection that contains documents representing places with a name and a location property. The location property is a geospatial point that represents the location of the place. Here's an example of how you can use a geospatial query to find all places within a certain distance of a given location:

var client = new DocumentClient(uri, { masterKey: key });

client.executeStoredProcedure('getLocationsNear', [latitude, longitude], function (err, results, responseHeaders) {
    if (err) {
        console.log(err);
    }
    else {
        var places = results;
        console.log(places);
    }
});

Explanation

Geospatial queries in CosmosDB allow you to search for documents based on their location. CosmosDB supports a variety of spatial queries, including radius queries, polygon queries, and distance queries.

To perform a geospatial query in CosmosDB, you need to define a stored procedure or a user-defined function that takes a location parameter. The procedure or function should then use a query to filter documents based on their proximity to the specified location.

Use

Geospatial queries in CosmosDB are useful for a variety of use cases, including location-based search, geofencing, and routing. By using geospatial queries, you can perform complex spatial analysis on your data and find relationships that would be difficult or impossible to detect using traditional query techniques.

Important Points

Here are some important points to keep in mind when using geospatial queries in CosmosDB:

  • CosmosDB supports a variety of spatial queries, including radius queries, polygon queries, and distance queries.
  • Geospatial queries in CosmosDB can be performed using stored procedures or user-defined functions.
  • To perform a geospatial query, you need to have a geospatial point property in your documents.

Summary

In this tutorial, we discussed how to use geospatial queries in CosmosDB. We covered the syntax, example, explanation, use, and important points of using geospatial queries in CosmosDB. With this knowledge, you can use geospatial queries in CosmosDB to perform complex spatial analysis on your data and find relationships that would be difficult or impossible to detect using traditional query techniques.

Published on: