|
| 1 | +.. _javars-geo: |
| 2 | + |
| 3 | +================= |
| 4 | +Geospatial Search |
| 5 | +================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +To support geospatial queries, MongoDB provides geospatial |
| 14 | +indexes and geospatial query operators. |
| 15 | + |
| 16 | +To learn more about performing geospatial queries, see |
| 17 | +:manual:`Geospatial Queries </geospatial-queries/>` in the |
| 18 | +Server manual. |
| 19 | + |
| 20 | +Prerequisites |
| 21 | +------------- |
| 22 | + |
| 23 | +You must set up the following components to run the code examples in |
| 24 | +this guide: |
| 25 | + |
| 26 | +- A ``test.restaurants`` collection populated with documents from the |
| 27 | + ``restaurants.json`` file in the `documentation assets GitHub |
| 28 | + <https://raw.githubusercontent.com/mongodb/docs-assets/drivers/restaurants.json>`__. |
| 29 | + |
| 30 | +- The following import statements: |
| 31 | + |
| 32 | + .. code-block:: java |
| 33 | + |
| 34 | + import com.mongodb.reactivestreams.client.MongoClients; |
| 35 | + import com.mongodb.reactivestreams.client.MongoClient; |
| 36 | + import com.mongodb.reactivestreams.client.MongoCollection; |
| 37 | + import com.mongodb.reactivestreams.client.MongoDatabase; |
| 38 | + import com.mongodb.client.model.geojson.*; |
| 39 | + import com.mongodb.client.model.Indexes; |
| 40 | + import com.mongodb.client.model.Filters; |
| 41 | + import org.bson.Document; |
| 42 | + |
| 43 | +.. important:: |
| 44 | + |
| 45 | + This guide uses the ``Subscriber`` implementations, which are |
| 46 | + described in the :ref:`Quick Start Primer <javars-primer>`. |
| 47 | + |
| 48 | +Connect to a MongoDB Deployment |
| 49 | +------------------------------- |
| 50 | + |
| 51 | +First, connect to a MongoDB deployment and declare and define |
| 52 | +``MongoDatabase`` and ``MongoCollection`` instances. |
| 53 | + |
| 54 | +The following code connects to a standalone |
| 55 | +MongoDB deployment running on ``localhost`` on port ``27017``. Then, it |
| 56 | +defines the ``database`` variable to refer to the ``test`` database and |
| 57 | +the ``collection`` variable to refer to the ``restaurants`` collection: |
| 58 | + |
| 59 | +.. code-block:: java |
| 60 | + |
| 61 | + MongoClient mongoClient = MongoClients.create(); |
| 62 | + MongoDatabase database = mongoClient.getDatabase("test"); |
| 63 | + MongoCollection<Document> collection = database.getCollection("restaurants"); |
| 64 | + |
| 65 | +To learn more about connecting to MongoDB deployments, |
| 66 | +see the :ref:`javars-connect` tutorial. |
| 67 | + |
| 68 | +Create the 2dsphere Index |
| 69 | +------------------------- |
| 70 | + |
| 71 | +To create a ``2dsphere`` index, use the ``Indexes.geo2dsphere()`` |
| 72 | +helper to create a specification for the ``2dsphere`` index. Pass the |
| 73 | +specification to the ``MongoCollection.createIndex()`` method to create |
| 74 | +the index. |
| 75 | + |
| 76 | +The following example creates a ``2dsphere`` index on the |
| 77 | +``"contact.location"`` field in the ``restaurants`` collection: |
| 78 | + |
| 79 | +.. code-block:: java |
| 80 | + |
| 81 | + MongoCollection<Document> collection = database.getCollection("restaurants"); |
| 82 | + collection.createIndex(Indexes.geo2dsphere("contact.location")) |
| 83 | + .subscribe(new PrintSubscriber<String>()); |
| 84 | + |
| 85 | +Query for Locations Near a GeoJSON Point |
| 86 | +---------------------------------------- |
| 87 | + |
| 88 | +MongoDB provides various geospatial query operators. To facilitate |
| 89 | +the creation of geospatial query filters, the driver provides |
| 90 | +the ``Filters`` class and the ``com.mongodb.client.model.geojson`` |
| 91 | +package. |
| 92 | + |
| 93 | +The following example returns documents that are at least 1000 meters |
| 94 | +and at most 5000 meters from the specified GeoJSON ``Point`` instance, |
| 95 | +sorted from nearest to farthest: |
| 96 | + |
| 97 | +.. code-block:: java |
| 98 | + |
| 99 | + Point refPoint = new Point(new Position(-73.9667, 40.78)); |
| 100 | + collection.find(Filters.near("contact.location", refPoint, 5000.0, 1000.0)) |
| 101 | + .subscribe(new PrintDocumentSubscriber()); |
0 commit comments