【问题标题】:Mongoose $near query returns parse errorMongoose $near 查询返回解析错误
【发布时间】:2015-05-06 07:54:31
【问题描述】:

这是一个对象示例:

{
    "_id" : "581994",
    "type" : "Feature",
    "geometry" : {
        "type" : "Point",
        "coordinates" : [
            -149.0133,
            64.7439
        ]
    }
}

这是我执行的查询:

        Earthquake
            .find({
                geometry: {
                    $near: {
                        $geometry: {
                            type: 'Point',
                            coordinates: [lon, lat]
                        }
                    }
                }
            })
            .exec(function(err, results) {

                if (err) {
                    console.log(err);
                }

                return reply(results);
            })

这是我创建的模型架构:

var mongoose = require('mongoose');

mongoose.set('debug', true);

var Schema = mongoose.Schema;

var earthquakeSchema = new Schema({
    geometry: {
        type: String,
        coordinates: [Number]
    }
});

earthquakeSchema.index({
    geometry: '2dsphere'
});

var Earthquake = mongoose.model('Earthquake', earthquakeSchema);

module.exports = Earthquake;

从我的角度来看,这似乎是正确的,但是当我执行它时,我总是会遇到同样的错误:

[Error: Can't use $near with String.]

我找不到错误在哪里。我到处检查过

【问题讨论】:

    标签: node.js mongodb mongoose geospatial


    【解决方案1】:

    好的,我找到了解决方案:

    我必须将与“Point”对应的默认字段添加到属性“type”。现在可以了

    【讨论】:

    • 请问你是怎么做到的?你能更新你的答案吗?
    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多