使用 Polygons 和 Mongoose 进行地理空间查询有什么问题?

发布于 2024-12-09 12:09:03 字数 2751 浏览 1 评论 0原文

我正在使用 $polygon 搜索进行地理空间查询。我的查询是:

{
    'location_latLong': {
        '$within': {
            '$polygon': {
                point1: {
                    lon: '-74.2621',
                    lat: '40.5788'
                },
                point2: {
                    lon: '-74.2621',
                    lat: '40.8494'
                },
                point3: {
                    lon: '-73.7499',
                    lat: '40.8494'
                },
                point4: {
                    lon: '-73.7499',
                    lat: '40.5788'
                }
            }
        }
    }
}

在我的集合中,我有一个文档明显位于多边形内:

{
    "_id": ObjectId("4e95c285cb8a0efc2b00001a"),
    "addedOn": Date(1318437509839),
    "checkinDate": Date(1318392000000),
    "checkinTime": Date(1318437476000),
    "location_city": "New York",
    "location_country": "United States",
    "location_latLong": {
        "xLon": -74.007124,
        "yLat": 40.71455
    },
    "location_source": "personprofile",
    "location_state": "New York",
    "location_zip": ""
}

并且我在 location_latLong 上有一个 2d 索引。我知道 MongoDB 会忽略键名(据说),所以这应该不重要。那么为什么会返回 0 结果呢?

编辑 我刚刚按照评论中的建议运行了一个 explain ,我得到:

{
    "cursor" : "GeoBrowse-polygon",
    "nscanned" : 0,
    "nscannedObjects" : 0,
    "n" : 0,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "location_latLong" : [
            [
                [
                    0,
                    0
                ],
                [
                    0.000021457672119140625,
                    0.000021457672119140625
                ]
            ],
            [
                [
                    -0.000021457672119140625,
                    -0.000021457672119140625
                ],
                [
                    0,
                    0
                ]
            ],
            [
                [
                    -0.000021457672119140625,
                    0
                ],
                [
                    0,
                    0.000021457672119140625
                ]
            ],
            [
                [
                    0,
                    -0.000021457672119140625
                ],
                [
                    0.000021457672119140625,
                    0
                ]
            ]
        ]
    },
    "keysChecked" : NumberLong(8),
    "lookedAt" : NumberLong(4),
    "matchesPerfd" : NumberLong(0),
    "objectsLoaded" : NumberLong(0),
    "pointsLoaded" : NumberLong(0)
}

I'm doing a geospatial query using a $polygon search. My query is:

{
    'location_latLong': {
        '$within': {
            '$polygon': {
                point1: {
                    lon: '-74.2621',
                    lat: '40.5788'
                },
                point2: {
                    lon: '-74.2621',
                    lat: '40.8494'
                },
                point3: {
                    lon: '-73.7499',
                    lat: '40.8494'
                },
                point4: {
                    lon: '-73.7499',
                    lat: '40.5788'
                }
            }
        }
    }
}

In my collection, I have a document that CLEARLY lies within the polygon:

{
    "_id": ObjectId("4e95c285cb8a0efc2b00001a"),
    "addedOn": Date(1318437509839),
    "checkinDate": Date(1318392000000),
    "checkinTime": Date(1318437476000),
    "location_city": "New York",
    "location_country": "United States",
    "location_latLong": {
        "xLon": -74.007124,
        "yLat": 40.71455
    },
    "location_source": "personprofile",
    "location_state": "New York",
    "location_zip": ""
}

and I have a 2d index on location_latLong. I know that MongoDB ignores key names (supposedly), so that shouldn't matter. So why does this return 0 results?

EDIT
I just ran an explain as recommended in the comments and I get:

{
    "cursor" : "GeoBrowse-polygon",
    "nscanned" : 0,
    "nscannedObjects" : 0,
    "n" : 0,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "location_latLong" : [
            [
                [
                    0,
                    0
                ],
                [
                    0.000021457672119140625,
                    0.000021457672119140625
                ]
            ],
            [
                [
                    -0.000021457672119140625,
                    -0.000021457672119140625
                ],
                [
                    0,
                    0
                ]
            ],
            [
                [
                    -0.000021457672119140625,
                    0
                ],
                [
                    0,
                    0.000021457672119140625
                ]
            ],
            [
                [
                    0,
                    -0.000021457672119140625
                ],
                [
                    0.000021457672119140625,
                    0
                ]
            ]
        ]
    },
    "keysChecked" : NumberLong(8),
    "lookedAt" : NumberLong(4),
    "matchesPerfd" : NumberLong(0),
    "objectsLoaded" : NumberLong(0),
    "pointsLoaded" : NumberLong(0)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2024-12-16 12:09:03

我解决了这个问题。我正在传递字符串,我应该传递浮点数。

感谢您的宝贵时间=)

I solved the problem. I was passing Strings and I should have been passing floats.

Thanks for your time =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文