gpt4 book ai didi

MongoDB:无法规范化查询:BadValue bad geo query

转载 作者:IT老高 更新时间:2023-10-28 13:24:57 24 4
gpt4 key购买 nike

我在 2dsphere 上索引了字段 loc,但无法对 Point 类型的 GeoJson 数据运行 geowithin 查询。

这里是查询:

 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 可以在相同的文档上正常工作。我想了解为什么 geowithin 不起作用?

最佳答案

这是因为你的多边形不是闭合的,你实际上至少需要四个点才能得到一个有效的多边形,第一个点在最后重复,见 GeoJSON Polygon docs .必须说,错误消息可能会更有帮助。 Well Known Text (WKT) 也是如此。和众所周知的二进制 (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:无法规范化查询:BadValue bad geo query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25893415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com