【问题标题】:CastError in mongoDB nearSphere method in mongoose 3.8.3mongoose 3.8.3 中 mongoDB nearSphere 方法中的 CastError
【发布时间】:2014-01-14 20:52:14
【问题描述】:

我在 mongoose 版本 3.8.3 中使用 mongoDB $nearSphere 方法来查找附近的位置。

mongoose.models['Event']
        .find({ loc : 
                { 
                    $nearSphere : 
                    { 
                        $geometry : loc,
                        $maxDistance : 100000
                    }
                }           
         })
        .limit(100)
        .exec(function (err, events)
        {
            console.log(err)
            ...
        }

Events Scheme 中的 loc 字段有一个 '2dsphere' 索引:

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

变量 loc 的类型为“Point”。一个例子是:

{ coordinates: [ 12.93598683338508, 48.43299197266921 ], type: 'Point' }

执行上面的搜索语句后总是报错:

2013-12-27T08:46:57.997102+00:00 app[web.1]: { message: 'Cast to number failed for value "Point" at path "__QueryCasting__"',
2013-12-27T08:46:57.997105+00:00 app[web.1]:   name: 'CastError',
2013-12-27T08:46:57.997107+00:00 app[web.1]:   type: 'number',
2013-12-27T08:46:57.997108+00:00 app[web.1]:   value: 'Point',
2013-12-27T08:46:57.997110+00:00 app[web.1]:   path: '__QueryCasting__' }

谁能弄清楚为什么会出现这个错误以及如何解决它?

【问题讨论】:

    标签: node.js mongodb mongoose geospatial geo


    【解决方案1】:

    这看起来像是 Mongoose 中的一个错误。我提出了一个问题: https://github.com/LearnBoost/mongoose/issues/1874

    我有两个解决方法要建议。

    我通过更改修补了 mongoose/lib/query.js:

    } else if (('$near' == geo || '$geoIntersects' == geo) &&
    

    } else if (('$near' == geo || '$nearSphere' == geo || '$geoIntersects' == geo) &&
    

    它对我有用(猫鼬 3.8.3)。

    其次,您可以将查询作为“传统”坐标对运行(您需要将 maxDistance 转换为弧度)。 http://docs.mongodb.org/manual/reference/operator/query/nearSphere/

    【讨论】:

    • 所以我有同样的问题,这里发生了什么。 Mongoose 现在是 3.8.8 这个问题已经解决了吗?我仍然拥有它。我实际上在 MongoDB shell 中运行该命令并且工作正常.. 是 Mongoose 不工作
    【解决方案2】:

    Mongoose 有一个名为 near() 的内置方法,它接受用于球形地理空间查询的 GeoJSON 点。

    来自文档:

    指定 $near 或 $nearSphere 条件

    所以,而不是:

    mongoose.models['Event']
        .find({ loc : 
                { 
                    $nearSphere : 
                    { 
                        $geometry : loc,
                        $maxDistance : 100000
                    }
                }           
         }).limit(100).exec(function (err, events) { ... });
    

    试试这个:

    mongoose.models['Event']
        .near('loc', {
            center: loc,
            maxDistance : 100000
        }).limit(100).exec(function (err, events) { ... });
    

    【讨论】:

      【解决方案3】:

      这并不是 mongoose 中的真正错误,您应该在 'loc.coordinates' 上执行 find(),而不仅仅是 'loc',因为这是 2dsphere 索引所在的字段。现在,即使 mongoose 强制执行此查询而没有失败,mongodb 服务器也会返回错误,因为您正在尝试在没有地理索引的字段上运行 $nearSphere。 FWIW 1 班轮修复将在 mongoose 的下一个次要版本中,但您还应该更改查询代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 2019-02-17
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        相关资源
        最近更新 更多