---
title: "Geographical queries with MongoDb"
canonical: "https://onesaitplatform.refined.site/space/DOCT/2220831114/Geographical%20queries%20with%20MongoDb"
format: markdown
---
We will now show how to conduct a geographic query on an ontology within Onesait Platform. To do this, first, there must be firstly an ontology with a Geometry-point type field where the location is stored. For this example a such ontology called **pubsOntology **has been created, where the location of different bars will be stored in the *location *field.

![image](media://78bbb766-4691-4415-8f8f-51e0001ef1bc)

To query which bar is closest to a specific location, the first step would be to indicate to MongoDb that the field where the location is being stored (in this case *location*) follows the GeoJSON structure. This is done by invoking the createIndex() command on the field where the location is being stored, and that has been previously defined as Geometry-point type.

![image](media://b0b76f15-7c63-4bdd-ab0c-d13ecd17fa9a)

The next step would be to formulate a find-type query that specifies that you want to know the point closest to a specific location. In the following image (from a viewer created from [plataforma](https://onesaitplatform.atlassian.net/wiki/spaces/OP/pages/45809698)), the three pubs that are registered (pub01, pub02, pub03) are represented, and the points in a circle show where the nearest pub is to the position.

![image](media://f5433543-bc89-40a7-9ea8-48f1bbb856fc)

In the case of circle number one, the query would be:

*db.pubsOntology.find({ 'pubsOntology.location': *

*{$near: {$geometry: {type: "Point",coordinates:[-1.130500,37.987087]},}}})*

The result is a list of bars, ordered from closest to furthest:

![image](media://0aea9024-101e-410b-b842-4214a814fecf)


As expected, the nearest bar would be Pub02, followed by Pub01, and Pub03. If we now change the location of the query to point 2, the result will be different:

![image](media://c6974228-dca5-4d52-92a8-5770c34e2656)

As you can see, in this case the closest bar would be Pub03.