MongoDB:Can';t规范化查询:BadValue错误地理查询

MongoDB:Can';t规范化查询:BadValue错误地理查询,mongodb,geospatial,geojson,2dsphere,Mongodb,Geospatial,Geojson,2dsphere,我为2dsphere上的字段loc编制了索引,无法对点类型GeoJson数据运行GeoInside查询 以下是查询: db.test.find( { loc : { $geoWithin : { $geometry : { type : "Polygon" , coordinates : [ [

我为2dsphere上的字段loc编制了索引,无法对点类型GeoJson数据运行GeoInside查询

以下是查询:

 db.test.find( { loc :
                     { $geoWithin :
                        { $geometry :
                           { type : "Polygon" ,
                             coordinates : [ [ [-74.6862705412253, 40.42341005] , 
                                               [-75.0846179, 39.9009465 ], 
                                               [-74.20570119999999, 41.0167639 ]
                                             ]
                                           ]
                           } 
                         } 
                      } 
                 } 
输出:

  uncaught exception: error: {
"$err" : "Can't canonicalize query: BadValue bad geo query",
"code" : 17287
}
文件结构:

         {
           "_id" : ObjectId("53d15e7132e7b7978c472e6e"),
           "loc" : {
                  "type" : "Point",
                   "coordinates" : [ -74.6862705412253, 40.42341005 ]
                   },

         }
索引:

{
"0" : {
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "name" : "_id_",
    "ns" : "collab.test"
},
"1" : {
    "v" : 1,
    "key" : {
        "loc" : "2dsphere"
    },
    "name" : "TestLocationIndex",
    "ns" : "collab.test",
    "2dsphereIndexVersion" : 2
}
}


但是,$Polygon在相同的文档上运行良好。我试图理解为什么Geoinsin不起作用

这是因为您的多边形不是闭合的,所以实际上,对于有效的多边形,您至少需要四个点,第一个点在末尾重复,请参见。必须指出,错误消息可能会更有帮助。这也适用于和众所周知的二进制(WKB)多边形格式,所以这不是GeoJSON的一个特性

您的查询应如下所示:

db.test.find({loc :
                {$geoWithin :
                    {$geometry :
                       {type : "Polygon" ,
                           coordinates : [[[-74.6862705412253, 40.42341005] , 
                                           [-75.0846179, 39.9009465], 
                                           [-74.20570119999999, 41.0167639],
                                           [-74.6862705412253, 40.42341005]]]                                     
                        } 
                    } 
                 } 
             } 

不客气。MongoDB的空间错误可能有点无益。