This is my first question here so please be patient with me if its not asked very well or if my english is bad.

I am trying do Geospatioal Querys in MongoDB in Node.js.

This query works just fine

collection.find( { loc : { $geoWithin :
    { $centerSphere :
        [ [ point.coordinates[0], point.coordinates[1]] , getRadiusMeters(radius) ]
    } } } )

but when i try to do the same with $nearSphere:

collection.find(  { loc :{ $nearSphere :
            { $geometry :
            { type : "Point" ,
                coordinates : [ point.coordinates[0], point.coordinates[1]] } ,
                $maxDistance : getRadiusMeters(radius)
            } } } )

I get the following Error:

{ [MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index),  for: { loc: { $nearSphere: { $geometry: { type: "Point", coordinates: [ 6.128777, 49.596792 ] }, $maxDistance: 10000 } } }] name: 'MongoError' }

I think i have the right indexes. Here are my Indexes:

> db.realtrip.getIndexes()
[
    {
            "v" : 1,
            "key" : {
                    "_id" : 1
            },
            "ns" : "test.realtrip",
            "name" : "_id_"
    },
    {
            "v" : 1,
            "key" : {
                    "loc" : "2dsphere"
            },
            "ns" : "test.realtrip",
            "name" : "loc_2dsphere"
    },
    {
            "v" : 1,
            "key" : {
                    "tags" : 1
            },
            "ns" : "test.realtrip",
            "name" : "tags_1"
    },
    {
            "v" : 1,
            "key" : {
                    "loc" : "2d"
            },
            "ns" : "test.realtrip",
            "name" : "loc_2d"
    }
]

I cant find what I am doing wrong. The $centerSphere Query works just fine only the $nearSphere Query gives me Errors.

有帮助吗?

解决方案

it turned out my bug was related to this issue Bugreport Mongodb

Updating my mongodb solved it. Now the code works as expected.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top