|
1 | 1 | ================= |
2 | 2 | Geospatial Search |
3 | 3 | ================= |
| 4 | + |
| 5 | +You can query against :manual:`geospatial indexes </geospatial-queries/#geospatial-indexes>` |
| 6 | +in several ways via the Node.js driver, using :manual:`geospatial query operators </reference/operator/query-geospatial/>` . |
| 7 | + |
| 8 | +To create a 2dsphere index on a collection, pass a document containing the name of the |
| 9 | +field to be indexed with the value '2dsphere' to the ``createIndex()`` method. |
| 10 | + |
| 11 | +.. code-block:: js |
| 12 | + |
| 13 | + const { MongoClient } = require('mongodb'); |
| 14 | + |
| 15 | + // Connection URL |
| 16 | + const url = 'mongodb://localhost:27017'; |
| 17 | + |
| 18 | + // Create a new MongoClient |
| 19 | + const client = new MongoClient(url); |
| 20 | + |
| 21 | + async function main(client) { |
| 22 | + const collection = client.db('test').collection('restaurants'); |
| 23 | + const result = await collection.createIndex({ 'address.coord' : '2dsphere' }); |
| 24 | + console.log(result); |
| 25 | + } |
| 26 | + |
| 27 | + // Function to connect to the server and run your code |
| 28 | + async function run() { |
| 29 | + try { |
| 30 | + // Connect the client to the server |
| 31 | + await client.connect(); |
| 32 | + console.log('Connected successfully to server'); |
| 33 | + |
| 34 | + await main(client); |
| 35 | + } finally { |
| 36 | + // Ensures that the client will close when you finish/error |
| 37 | + await client.close(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Runs your code |
| 42 | + run(); |
| 43 | + |
| 44 | +The following examples assume that a database called ``test`` has a |
| 45 | +collection called ``restaurants``\ , with a :manual:`2d sphere index </core/2dsphere/>` |
| 46 | +index on the ``address.coord`` field. A |
| 47 | +`sample dataset <https://docs.mongodb.org/getting-started/node/import-data/>`_ is available for download. |
| 48 | + |
| 49 | +$near |
| 50 | +----- |
| 51 | + |
| 52 | +The :manual:`$near </reference/operator/query/near/>` operator specifies |
| 53 | +a set of longitude-latitude coordinates and returns documents from nearest to farthest. |
| 54 | + |
| 55 | +.. code-block:: js |
| 56 | + |
| 57 | + async function main(client) { |
| 58 | + const collection = client.db('test').collection('restaurants'); |
| 59 | + const docs = await collection |
| 60 | + .find({ |
| 61 | + 'address.coord': { |
| 62 | + $near: { |
| 63 | + $geometry: { |
| 64 | + type: 'Point', |
| 65 | + coordinates: [ -73.9667, 40.78 ] |
| 66 | + }, |
| 67 | + $maxDistance: 1000 |
| 68 | + } |
| 69 | + } |
| 70 | + }) |
| 71 | + .toArray(); |
| 72 | + |
| 73 | + console.log('Found the following records'); |
| 74 | + console.log(docs); |
| 75 | + } |
| 76 | + |
| 77 | +The ``$maxDistance`` option specifies a maximum distance (in meters) from the given |
| 78 | +coordinates. For a complete list of ``$near`` options, see the |
| 79 | +:manual:`MongoDB manual </reference/operator/query/near/>` . |
| 80 | + |
| 81 | +$geoWithin |
| 82 | +---------- |
| 83 | + |
| 84 | +The :manual:`$geoWithin </reference/operator/query/geoWithin/>` operator |
| 85 | +selects documents with geospatial data that exist within a specified shape. |
| 86 | + |
| 87 | +.. code-block:: js |
| 88 | + |
| 89 | + async function main(client) { |
| 90 | + const collection = client.db('test').collection('restaurants'); |
| 91 | + const docs = await collection |
| 92 | + .find({ |
| 93 | + 'address.coord': { |
| 94 | + $geoWithin: { |
| 95 | + $geometry: { |
| 96 | + type: 'Polygon', |
| 97 | + coordinates: [ |
| 98 | + [ [ -73, 40 ], [ -74, 41 ], [ -72, 39 ], [ -73, 40 ] ] |
| 99 | + ] |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + }) |
| 104 | + .toArray(); |
| 105 | + |
| 106 | + console.log('Found the following records'); |
| 107 | + console.log(docs); |
| 108 | + } |
0 commit comments