繁体   English   中英

mongo进行$ near查找时出现范围错误

[英]Range Error with mongo doing a $near lookup

mongo在$near查找中遇到奇怪的错误。 DB疯了,吐出了我们的量程错误。

下面的代码片段可以正常工作

mongoose.models.Users.findOne({_id: req.user.id}, function(err, loggedin) {
    var location = JSON.parse(JSON.stringify(loggedin.location));
    mongoose.models.Users.find({
      location: { $near: { $geometry: location } },
    }, (err, users) => {

    })
})

但是,如果我搞错了JSON.parse(JSON.stringify())克隆它吓坏了

mongoose.models.Users.findOne({_id: req.user.id}, function(err, loggedin) {
    var location = loggedin.location;
    mongoose.models.Users.find({
      location: { $near: { $geometry: location } },
    }, (err, users) => {

    })
})

如您所见,使用猫鼬,但这不会影响此查询。 我必须看不到某些额外的location ,但找不到。

如果我在数据库查询后立即console.log loggedin.location我得到这个:

location: { type: 'Point', coordinates: [ -118.3141928, 34.0806176 ] },

我还设置了一个调试断点,并得到了相同的结果

另一个值得注意的事实是,如果我使用lean()执行findOne查询,因此它仅返回纯JSON,则第二个查询可以完美地工作。 我想知道访问geoJSON属性时是否有猫鼬调用的吸气剂。

我的猫鼬模式设置如下

const UserSchema = new Schema({
  location: {
    type: { type: String },
    coordinates: [],
  },
})
userSchema.index({ location: '2dsphere' });

尝试在不进行额外分配的情况下尝试执行此操作,以及像这样简单地运行查询

location: { $near: { $geometry: loggedin.location } },

但是得到了同样的错误。

另一个有趣的事实是,如果我使用mongoose.models.Users.collection进入本机客户端并执行查询,那么我根本不会收到错误。

具有mongo shell输出和本机查询的示例要点

试试看这段代码

 $geoNear: {
            near: {type: "Point", coordinates:[parseFloat(req.query.latitude), parseFloat(req.query.longitude )]},
            distanceField: "dist.calculated",
            maxDistance:10000,
            spherical: true
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM