Mongoose near(…) query on 2dsphere indexed field not returning valid results

前端 未结 1 541
情深已故
情深已故 2020-12-25 08:04

I am unable to query a mongodb database using mongoose\'s qry.where().near() syntax.

I have a Schema with coordinates stored as an array, indexed as

相关标签:
1条回答
  • 2020-12-25 09:09

    Seems this is a moongoose bug.

    Changing the query to use a GeoJSON object instead of a coordinate pair, as such:

    qry.where('loc').near({
        center: {
            type: 'Point',
            coordinates: search.loc
        },
        maxDistance: search.distance * 1000
    });
    

    results in the following query:

    Mongoose: models.find({ loc: { '$near': { 
            '$maxDistance': 1,
            '$geometry': { type: 'Point', coordinates: [ 10, -20 ] } } } 
        }) { fields: undefined }  
    

    The search now succeeds.

    The docs explicitly show a query using a coordinate pair:

    query.where('loc').near({ center: [10, 10], maxDistance: 5 });
    

    However, it looks like this doesn't work, and the example should be:

    query.where('loc').near({ center: { coordinates: [10, 10], type: 'Point' }, maxDistance: 5 });
    
    0 讨论(0)
提交回复
热议问题