In my lootPoint
collection I have the data like this:
{
"_id" : "1856132932",
"point" : {
"type" : "Point",
"coordinates" : [
21.6550408,
50.1034841
]
}
}
{
"_id" : "2184534902",
"point" : {
"type" : "Point",
"coordinates" : [
21.6560194,
50.1037961
]
}
}
I want to query for a random point, matching the square with the circular 20m cut-out in the middle:
Using Internet and AI I managed to assemble the following:
db.lootPoint.aggregate([
{
$match: {
"point.coordinates": {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[21.654939727403415,50.10333291665514],
[21.656736672596585,50.10333291665514],
[21.656736672596585,50.10453288334485],
[21.654939727403415,50.10453288334485],
[21.654939727403415,50.10333291665514]
]
]
}
},
$not: {
$geoWithin: {
$centerSphere: [[21.6558382, 50.1039329], 20 / 3963.2]
}
}
}
}
},
{ $sample: { size: 1 } }
])
Upon running this command in Robo3T I'm getting the error:
I tried re-writing the query like with $and and $not so:
db.lootPoint.aggregate([
{
$match: {
$and:[
{ "point.coordinates": {...} },
{ "point.coordinates": { $not:[ ... ] } },
]
}
},
...
but it brings up 0 results.
What am I doing wrong? How to make this query valid? Are there any alternative approaches?
20
is also kinda too much for 20 m distance, and should be0.02
. Feel free to post your sample as an answer!