【问题标题】:Mongoose near(...) query on 2dsphere indexed field not returning valid resultsMongoose near(...) 对 2dsphere 索引字段的查询未返回有效结果
【发布时间】:2014-05-30 14:29:44
【问题描述】:

我无法使用 mongoose 的 qry.where().near() 语法查询 mongodb 数据库。

我有一个坐标存储为数组的架构,索引为2dsphere

loc : { type: [Number], index: '2dsphere' }

我正在使用.where().near() 运行猫鼬查询:

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

我已经启用mongoose.set('debug', true),并且可以看到调试结果:

在集合中插入2个坐标为[ 10, -20 ]的文档:

Mongoose: models.insert({ _id: ..., loc: [ 10, -20 ], type: 'apple' }) {}  
Mongoose: models.insert({ _id: ..., loc: [ 10, -20 ], type: 'orange' }) {}  

搜索坐标[ 10, -20 ]附近的文档:

Mongoose: models.find({ loc: { '$near': [ 10, -20 ], '$maxDistance': 1000 } })

查询返回没有结果。

我已经通过搜索type 证明了这些文档在数据库中。它是我无法工作的地理空间查询。

我正在使用mongodb v2.6.1mongoose v3.8.8

下面的完整工作示例:

架构:(model.js)

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ModelSchema = new Schema({
    type : { type: String, index: true },
    loc  : { type: [Number], index: '2dsphere' }
});

ModelSchema.index({type: 1, loc: 1});

ModelSchema.statics.search = function(search, cb) {
    var qry = this.find();
    if (search.types) {
        qry.where('type').in(search.types);
    }
    if (search.loc) {
        qry.where('loc').near({
            center: search.loc,
            maxDistance: search.distance * 1000
        });
    }
    qry.exec(cb);
};

摩卡测试:(test.ts)

var q = require('node-promise'),
    should = require('should'),
    mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/my-test');
var Model = require('./model');

describe('My Model', function() {
    before(function(done) {
        mongoose.set('debug', true);

        Model.remove().exec(function() {
            var create = function(promises, body) {
                var p = new q.Promise();
                var m = new Model(body);
                m.save(function(err) {
                    if (err) {
                        console.log(err);
                        p.reject();
                    } else {
                        p.resolve();
                    }
                });
                promises.push(p);
            };

            var promises = [];
            create(promises, { type: 'apple',  loc: [ 10, -20 ] });
            create(promises, { type: 'orange', loc: [ 10, -20 ] });
            create(promises, { type: 'apple',  loc: [ 15,  10 ] });
            create(promises, { type: 'orange', loc: [ 15,  10 ] });
            q.all(promises).then(function() {
                done();
            })
        });
    });

    it('should find all', function(done) {
        Model.search({}, function(err, items) {
            items.should.have.length(4);
            done();
        });
    });

    it('should find apples', function(done) {
        Model.search({types: ['apple']}, function(err, items) {
            items.should.have.length(2);
            done();
        });
    });

    // this test fails
    it('should find coords', function(done) {
        Model.search({loc: [ 10, -20 ], distance: 1}, function(err, items) {
            items.should.have.length(2);
            done();
        });
    });

});

package.json:

{
  "name": "test",
  "version": "0.0.0",
  "dependencies": {
    "mongoose": "~3.8.8"
  },
  "devDependencies": {
    "should": "~2.1.0",
    "node-promise": "0.5.10"
  }
}

跑摩卡:

sudo npm install -g mocha
npm install
mocha ./test.js

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    似乎这是一个moongoose bug

    将查询更改为使用GeoJSON 对象而不是坐标对,如下所示:

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

    产生以下查询:

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

    现在搜索成功。

    docs 明确显示使用坐标对的查询:

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

    不过好像这样不行,例子应该是:

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

    【讨论】:

    • 如果您的索引是 2dsphere,则必须使用 GeoJSON 表示法,如果只是 2d,则需要使用坐标对。
    • 你,@Steve,是救生员!
    • 如何显示准确的距离?示例:user.distance = 2230 米。可以按距离订购吗?
    • @IgorMartins 你找到答案了吗?
    猜你喜欢
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多