Created
July 18, 2018 13:49
-
-
Save vkarpov15/b4d6c924883b8b2e62e4a6285bf8eded to your computer and use it in GitHub Desktop.
Example of querying a GeoJSON feature collection in MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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