简体   繁体   English

如何在 Mongoose 中使用地理空间查询检索文档

[英]How to retrieve document using geospatial query in Mongoose

I am trying to use mongoose geospatial queries on the mongodb collection.我正在尝试在 mongodb 集合上使用猫鼬地理空间查询。 "2dsphere" index is already applied on the location object. “2dsphere”索引已应用于位置对象。

Query询问

Please find the query below:请在下面找到查询:

const data = await Banks.find(
    {
        location: {
            $near: {
                $geometry: {
                    type: "Point",
                    coordinates: [73.8567, 18.5204]
                },
                $maxDistance: 10000000
            }
        }
    }
);

console.log('-- the data --', data);

Data数据

One of the many documents looks like below:许多文件之一如下所示:

{
    "_id" : ObjectId("5c64511a04ede20c28f6ef0b"),
    "location" : {
        "coordinates" : [ 
            92.747338, 
            11.675442
        ],
        "type" : "Point"
    },
    "name" : "Test name",
}

What I've tried我试过的

I have tried to set max distance as big as possible.我试图将最大距离设置得尽可能大。 But the query does not retrieve any documents back.但是查询不会检索回任何文档。 What could be the problem?可能是什么问题呢?

The query mentioned above works fine.上面提到的查询工作正常。 I had been referring to wrong Model Name(collection).我一直在指错误的模型名称(集合)。 The collection name was banks and I had been referring as Banks.集合名称是银行,我一直指的是银行。 The Model Name's are case sensitive in MongoDB.模型名称在 MongoDB 中区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM