I have documents as the one below stored in MongoDB 3.0.5
{
"_id" : ObjectId("55f3a6ae0907233b5e9e7da0"),
"user" : {
"id" : "2d5b093ec8a3",
"regions" : [
{
"id" : NumberLong(1442365443852),
"name" : "penta",
"type" : "Polygon",
"center" : {
"lat" : -12.1254159880008100,
"lng" : -77.0316830277442930
},
"zoom" : 17.0000000000000000,
"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
]
]
},
{
"id" : NumberLong(1442366496200),
"name" : "triangle",
"type" : "Polygon",
"center" : {
"lat" : -12.1254749913046230,
"lng" : -77.0316598936915400
},
"zoom" : 17.0000000000000000,
"coordinates" : [
[
-77.0313568040728570,
-12.1266573492018090
],
[
-77.0325788855552670,
-12.1246968022373030
],
[
-77.0300653204321860,
-12.1246233756874440
],
[
-77.0313568040728570,
-12.1266573492018090
]
]
}
]
}
}
which represents an array of google map regions stored as polygons. I am trying to query them with several alternatives but none seems to work so I am wondering if geospatial MongoDB queries works on arrays.
My Java code is: DBCursor docs = getCollection().find(search);
All the following 6 'search' queries return no results when geometrically I would expect a match:
{ "user.regions" : { "$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]]]}}}}
{ "user.regions.0" : { "$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]]]}}}}
{ "user.regions.0.center" : { "$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]]]}}}}
{ "user.regions" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [ -77.03457042574883 , -12.123750122669545] , [ -77.03457042574883 , -12.12736341792724] , [ -77.02877718955278 , -12.12736341792724] , [ -77.02877718955278 , -12.123750122669545]]]}}}}
{ "user.regions.0" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [ -77.03457042574883 , -12.123750122669545] , [ -77.03457042574883 , -12.12736341792724] , [ -77.02877718955278 , -12.12736341792724] , [ -77.02877718955278 , -12.123750122669545]]]}}}}
{ "user.regions.0.center" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [ -77.03457042574883 , -12.123750122669545] , [ -77.03457042574883 , -12.12736341792724] , [ -77.02877718955278 , -12.12736341792724] , [ -77.02877718955278 , -12.123750122669545]]]}}}}
I think I am respecting the long/lat order in MongoDB, I repeat the closing point in my polygons, I have added type="Polygon" and in the case of the center point it is under lat/lng params. But no results.
I have not created any indices and I think I am matching the syntax for both params geoWithin (http://docs.mongodb.org/manual/reference/operator/query/geoWithin/) and geoIntercepts (http://docs.mongodb.org/manual/reference/operator/query/geoIntersects/)
{
<location field>: {
$geoWithin: {
$geometry: {
type: <"Polygon" or "MultiPolygon"> ,
coordinates: [ <coordinates> ]
}
}
}
}
{
<location field>: {
$geoIntersects: {
$geometry: {
type: "<GeoJSON object type>" ,
coordinates: [ <coordinates> ]
}
}
}
}
Regarding 'location field' I followed instructions for arrays as said here http://docs.mongodb.org/manual/tutorial/query-documents/
But nothing makes the queries get any result. I googled for same case and I just could find this unsolved question from 2013: How do I perform a find using $geoIntersects / 2dsphere in an array subfield?
So, unless I am doing a common mistake I am wondering if is possible to make geospatial queries to array params?
Any suggestions?
Thanks