Skip to content

Instantly share code, notes, and snippets.

@vkarpov15
Created July 18, 2018 13:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vkarpov15/b4d6c924883b8b2e62e4a6285bf8eded to your computer and use it in GitHub Desktop.
Save vkarpov15/b4d6c924883b8b2e62e4a6285bf8eded to your computer and use it in GitHub Desktop.
Example of querying a GeoJSON feature collection in MongoDB
db.test.drop();
db.test.insertOne({
name: 'Denver',
location: {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-104.9903,
39.7392
]
}
}
]
}
});
db.test.createIndex( { "location.features.geometry": "2dsphere" } )
const doc = db.test.findOne({
'location.features.geometry': {
$geoIntersects: {
$geometry: {
"type": "Polygon",
"coordinates": [[
[-109, 41],
[-102, 41],
[-102, 37],
[-109, 37],
[-109, 41]
]]
}
}
}
});
print(doc.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment