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