Using NoSQL DB with Geolocation in Node.js

Blumareks
5 min readSep 14, 2020
Locating Points of Interest (PoIs) based on the Geolocation

This blog post shows how you can use Cloudant NoSQL from Node.js application to get records in the specified GEO located shape (within a radius, or even inside a polygon shape).

Try Cloudant for FREE!

Try Cloudant NoSQL DB yourself for free — just use this link to invoke IBM Cloud registration/signup/login page (this URL gives an author = me some brownie points): https://ibm.biz/Bd2CUaf

This is a first part of the blog post defining the NoSQL DB and the Geolocated documents, as well as using the Dashboard of Cloudant DB. The second part would use Node.JS to extract the data in an app.

Why Cloudant?

Cloudant — a “Data Base as a Service” — is appealing to a startup developer. It is like a hold-it-all “duffel” bag in which you could store all the needed data items. One can add new items, search for them, update them and remove them.
Because Cloudant DB is maintenance less — provided to you as SaaS — it is extremely good for a STARTUP developer/CTO/Founder , but it is also being used by entreprises from small to top 500.

And Cloudant is so remarkably awesome at rapid development. For example if you change something in your data model, you do not need to ask anyone for help: to change schemas, indexing, etc. (a cliche: in a startup world you quickly learn that you can’t rely on anyone else but you, at least in the very beginning — and is that so different if one were in the corpo shoes?).

Cloudant uses just JSON and it is simply NOSQL. In the very beginning at the stage of creating a Minimal Viable Product* (MVP) Cloudant DB is just a perfect solution, and is made available to anyone as DBaaS just like that instantly — and at the IBM industry best — on the enterprise grade and free PaaS**. If your tongue didn’t break on those nosql_dbaas_paas abbreviations — that is good — these terms simply mean that what you need to work with Cloudant is O.N.L.Y. an Internet connection, because IBM is keeping the servers (the physical part), the engine (the software), the data (the storage libraries = more hardware), and overall management (the quality of service, the IT maintenance of the hardware, software, and the backups) on your behalf.

*MVP — the product with a minimal set of features that starts to sell — think about iPad version 1, the first NEST sensor, or an Apple watch.
**IBM Cloud is free of charge with Lite development accounts and it is competitively priced with a free tier for Pay-as-you-Go accounts.
***Friends and Family — the first early seed investors.

Cloudant DB on IBM Cloud

What is Cloudant? Cloudant is a hosted and managed distributed DBaaS — “database-as-a-service”, based on a horizontally scalable version of Apache CouchDB and other tools. In this example I am using Cloudant that is based on a “Cloud” — on IBM Cloud.

A Cloudant database is NOSQL — a schemaless JSON document store shared across a set of nodes (a la Dynamo), featuring incrementally-updated MapReduce Views, secondary key indexing, built-in full text search (based on Lucene), geo-spatial indexing, multi-master replication (supports global data distribution) and communicates over an HTTP RESTful API. Cloudant hosts and completely manages your database, including 24-hour support, at a fraction of the cost of a full-time administrator.

The data with various geolocation.

If your app is about local restaurants, local grocery stores, coffee shops, or gas stations — the important part is to get the information that is geo located and filtered by a distance from you, and based on your current longitude latitude. More information on the setting up your Cloudant DB with Geospacial information can be found here: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-cloudant-nosql-db-geospatial

Querying an IBM Cloudant Geo index — Simple circle

This simple example demonstrates how IBM Cloudant Geo can find documents that are considered to have a geospatial position within a given geographic circle. The function might be useful to determine insurance customers who live close to a known flood plain, grocery stores, or coffee shops near you.

To specify the circle, you provide the following values:

Latitude
Longitude
Circle radius, which is specified in meters

This query compares the geometry of each document in the index with the geometry of the specified circle. The comparison is run according to the relation you request in the query. So, to find all documents that fall within the circle, you use the contains relation.

You can also use polygons, for more information check it out here: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-cloudant-nosql-db-geospatial#a-polygon-query

GeoJSON functionality is unique to Cloudant. See the attached picture for the details:

Geospatial is unique to Cloudant DB (not CouchDB)

It is pretty easy to query documents based on number of constraints. The Cloudant management dashboard provides in depth analysis tools to do so. Follow this instructions to replicate the example crime database from Boston, and check the results: https://sharynr.github.io/ibm-cloudant-lab/geospatial.html

Replicating the database of the locations is possible with the replication tool in Cloudant DB. The screen below demonstrates the setup of the replication, the Authentication will use USER and PASSWORD for the local DB (at the moment it is not specified).

Specifying the details of the replication
The process of replications finished successfully

Example crime documents shown on the map.

The crime scenes pinned on the map.

It is possible to look up the details of the documents by their JSON contents.

Some crime documents with the GeoJSON tags

Now, based on the gathered data you can easily find the documents near your location (in 500m radius) — searching for crime scenes near you:

Looking for documents on crimes in Boston within 500m radius from a point

The results are shown on the following screenshot:

Finding out 3 crime scenes within 500m radius

What is next?

Now let’s search and read the documents based on the Geolocation from an app. Learn more about geospatial with Cloudant here:

See more information on code examples with Node.js (other languages also available): https://cloud.ibm.com/docs/node?topic=node-cloudant#prereqs-cloudant

If you like this short blogpost — please subscribe to my twitter account: https://twitter.com/blumareks/

--

--

Blumareks

I am a technology advocate for autonomous robots, AI, mobile and Internet of Things - with a view from both the enterprise and a robotics startup founder.