Domanda

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.

È stato utile?

Soluzione

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top