Reputation: 179
I'm performing this query
{
"$geoNear":{
"uniqueDocs":true,
"includeLocs":true,
"near":[
8.759131,
40.272393
],
"spherical":false,
"distanceField":"d",
"maxDistance":0.09692224622030236,
"query":{
},
"num":3
}
}
On this Model:
var ridePathSchema = new Schema({
...
loc: [Number],
...
});
ridePathSchema.index({
loc: "2d"
});
And I get:
Unhandled rejection reason: MongoError:
can't find any special indices: 2d (needs index), 2dsphere (needs index)
The funny thing is that just before this query I do perform a similar query on the same model but I do aggregate on it and it works.
What am I doing wrong?
UPDATE:
RidePaths.aggregate([query]) WORKS
RidePaths.find(query) CAN'T FIND INDEX
Upvotes: 2
Views: 2420
Reputation: 10790
$geoNear should be executed either as a command or in the first stage of an aggregation query (as you've noted).
Commands cannot be passed to mongooses find() method. The only way to issue commands is through the driver interface. See this gist for a working example of how to do so: https://gist.github.com/aheckmann/5871847
Keep in mind that results from the driver interface will not be mongoose documents since mongoose is being bypassed.
Upvotes: 5