简体   繁体   中英

How to retrieve document using geospatial query in Mongoose

I am trying to use mongoose geospatial queries on the mongodb collection. "2dsphere" index is already applied on the location object.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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