- Tutorials
- Geospatial Search
Geospatial Search
You can query against geospatial indexes in several ways via the Node.js driver, using geospatial query operators.
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.
function create2dSphereIndex(db, callback) {
// Get the restaurants collection
const collection = db.collection('restaurants');
// Create the index
collection.createIndex(
{ 'address.coord' : "2dsphere" }, function(err, result) {
console.log(result);
callback(result);
});
};
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Create a new MongoClient
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function(err, client) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
create2dSphereIndex(db, function() {
client.close();
});
});
The following examples assume that a database called test
has a
collection called restaurants
, with a 2d sphere index
index on the address.coord
field. A
sample dataset is available for download.
$near
The $near operator specifies a set of longitude-latitude coordinates and returns documents from nearest to farthest.
function findDocuments(db, callback) {
// Get the documents collection
const collection = db.collection('restaurants');
// Find some documents
collection.find(
{ 'address.coord':
{ $near :
{ $geometry:
{ type: "Point", coordinates: [ -73.9667, 40.78 ] },
$maxDistance: 1000
}
}
}
).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
}
// use the findDocuments() function
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function(err, client) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
findDocuments(db, function() {
client.close();
});
});
The $maxDistance
option specifies a maximum distance (in meters) from the given
coordinates. For a complete list of $near
options, see the
MongoDB manual.
$geoWithin
The $geoWithin operator selects documents with geospatial data that exist within a specified shape.
function findDocuments(db, callback) {
// Get the documents collection
const collection = db.collection('restaurants');
// Find some documents
collection.find(
{ 'address.coord':
{ $geoWithin:
{ $geometry:
{ type : "Polygon" ,
coordinates: [ [ [ -73, 40 ], [ -74, 41 ], [ -72, 39 ], [ -73, 40 ] ] ]
}
}
}
}
).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
}
// use the findDocuments() function
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function(err, client) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
findDocuments(db, function() {
client.close();
});
});