MongoDB can't parse query (2dsphere): $geoWithin:
By : michael ou
Date : March 29 2020, 07:55 AM
around this issue The polygon that you are providing for $geowithin query is incorrect. A polygon needs to have the same start and end point as per GeoJSON definition. The correct query is: code :
db.myCollection.find( { 'location.geometry':
{ '$geoWithin':
{ '$geometry' :
{ 'type' : "Polygon",
'coordinates' : [
[ -118.108006, 34.046072],
[ -117.978230, 34.041521],
[ -117.987328,33.913645 ],
[ -118.108006, 34.046072]
]
}
}
}
}
);
|
Difference in $geoWithin and $geoIntersects operators?
By : user3558815
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , From $geoIntersects: code :
> db.test.drop()
> var poly1 = { "type" : "Polygon", "coordinates" : [[[0, 0], [3, 0], [0, 3], [0, 0]]] }
> var poly2 = { "type" : "Polygon", "coordinates" : [[[1, 1], [2, 1], [1, 2], [1, 1]]] }
// poly1 is a similar triangle inside poly2
> var poly3 = { "type" : "Polygon", "coordinates" : [[[1, 0], [-2, 0], [1, 3], [1, 0]]] }
// poly3 is poly1 flipped around its "vertical" edge, then bumped over one unit, so it intersects poly1 but is not contained in it
> db.test.insert({ "loc" : poly2 })
> db.test.insert({ "loc" : poly3 })
> db.test.ensureIndex({ "loc" : "2dsphere" })
> db.test.find({ "loc" : {
"$geoIntersects" : {
"$geometry" : poly1
}
} })
// poly2 and poly3 returned
> db.test.find({ "loc" : {
"$geoWithin" : {
"$geometry" : poly1
}
} })
// poly2 returned
|
Do geospatial queries work on arrays? ( $geoWithin, $geoIntersects )
By : Yang
Date : March 29 2020, 07:55 AM
I hope this helps . This is one of those both yes and no questions to answer, as yes an array is supported for matching results in but it is also probably not what you really want considering the restrictions on how the matching is done. code :
{
"regions": [
{
"name": "penta",
"geometry": {
"type": "Polygon",
"coordinates": [[
[
-77.0322804898023610,
-12.1271067552781560
],
[
-77.0336792618036270,
-12.1255133434450870
],
[
-77.0326449349522590,
-12.1239143495252150
],
[
-77.0300991833209990,
-12.1238251884504540
],
[
-77.0299865305423740,
-12.1262000752832540
],
[
-77.0322804898023610,
-12.1271067552781560
]
]]
}
},
{
"name": "triangle",
"geometry": {
"type": "Polygon",
"coordinates": [[
[
-77.0313568040728570,
-12.1266573492018090
],
[
-77.0325788855552670,
-12.1246968022373030
],
[
-77.0300653204321860,
-12.1246233756874440
],
[
-77.0313568040728570,
-12.1266573492018090
]
]]
}
}
]
}
db.regions.createIndex({ "regions.geometry": "2dsphere" })
db.regions.find({
"regions.geometry" : {
"$geoIntersects" : {
"$geometry" : {
"type" : "Polygon" ,
"coordinates" : [[
[ -77.02877718955278 , -12.123750122669545],
[ -77.03457042574883 , -12.123750122669545],
[ -77.03457042574883 , -12.12736341792724],
[ -77.02877718955278 , -12.12736341792724],
[ -77.02877718955278 , -12.123750122669545]
]]
}
}
}
})
db.shapes.find({
"geometry" : {
"$geoIntersects" : {
"$geometry" : {
"type" : "Polygon" ,
"coordinates" : [ [
[ -77.02877718955278 , -12.123750122669545],
[ -77.03457042574883 , -12.123750122669545],
[ -77.03457042574883 , -12.12736341792724],
[ -77.02877718955278 , -12.12736341792724],
[ -77.02877718955278 , -12.123750122669545]
]]
}
}
}
})
{
"_id" : ObjectId("55f8d2fa66c2e7c750414b7a"),
"name" : "penta",
"geometry" : {
"type" : "Polygon",
"coordinates" : [[
[
-77.03228048980236,
-12.127106755278156
],
[
-77.03367926180363,
-12.125513343445087
],
[
-77.03264493495226,
-12.123914349525215
],
[
-77.030099183321,
-12.123825188450454
],
[
-77.02998653054237,
-12.126200075283254
],
[
-77.03228048980236,
-12.127106755278156
]
]]
}
}
{
"_id" : ObjectId("55f8d2fa66c2e7c750414b7b"),
"name" : "triangle",
"geometry" : {
"type" : "Polygon",
"coordinates" : [[
[
-77.03135680407286,
-12.126657349201809
],
[
-77.03257888555527,
-12.124696802237303
],
[
-77.03006532043219,
-12.124623375687444
],
[
-77.03135680407286,
-12.126657349201809
]
]]
}
}
|
mongodb $geoIntersects or $geoWithin a point matches multiple polygons - sort by area?
By : Shale.Xiong
Date : March 29 2020, 07:55 AM
I wish this help you So I solved this in a different way. Calculating the areas of geography objects should be done ahead of time, when populating the index. I wrote code to calculate area and bounds for each polygon and insert in the collection, so a conventional sort can be used, along with its indexes. code :
db.collection.find({
loc: {$geoInteresects: {$geometry: {
type: 'Point',
coordinates: [lng, lat]
}}}).sort({'properties.area': 1})
|
MongoDB $geoWithin $centerSphere query
By : Murad.BUmmos
Date : March 29 2020, 07:55 AM
This might help you The code is perfect! Just the coordinates value are not saved as Number. So the Schema should become: code :
var schema = new Schema({
title : { type: String, default: "generic"},
guide : String,
loc : {
type : {type: String, default:"Point"},
coordinates : [ { Number } ]
},
}
);
"loc": {
"coordinates": [
34,
22
],
"type": "Point"
},
"loc": {
"coordinates": [
"34",
"22"
],
"type": "Point"
},
|