[英]Range Error with mongo doing a $near lookup
Experiencing a weird error with mongo doing a $near
lookup. mongo在
$near
查找中遇到奇怪的错误。 The DB just goes crazy and spits our a range error. DB疯了,吐出了我们的量程错误。
This code snippet below works perfectly fine 下面的代码片段可以正常工作
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) => {
})
})
However, if I tallk out the JSON.parse(JSON.stringify()) clone it freaks out 但是,如果我搞错了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) => {
})
})
Using mongoose as you see but that shouldnt effect this query. 如您所见,使用猫鼬,但这不会影响此查询。 There must be something extra in
location
that I cant see but I cant find it. 我必须看不到某些额外的
location
,但找不到。
If I console.log loggedin.location
immediately after the DB query I get this: 如果我在数据库查询后立即console.log
loggedin.location
我得到这个:
location: { type: 'Point', coordinates: [ -118.3141928, 34.0806176 ] },
I've also set a debug breakpoint and get the same thing 我还设置了一个调试断点,并得到了相同的结果
One other fact thats interesting to note, if I execute the findOne
query with lean()
so it only returns plain JSON then the second query works perfectly. 另一个值得注意的事实是,如果我使用
lean()
执行findOne
查询,因此它仅返回纯JSON,则第二个查询可以完美地工作。 I'm wondering if theres some getter that is called by mongoose when accessing a geoJSON property. 我想知道访问geoJSON属性时是否有猫鼬调用的吸气剂。
My Mongoose schema is set up like so 我的猫鼬模式设置如下
const UserSchema = new Schema({
location: {
type: { type: String },
coordinates: [],
},
})
userSchema.index({ location: '2dsphere' });
Attempted to try this without the extra assignment as well running the query simply as 尝试在不进行额外分配的情况下尝试执行此操作,以及像这样简单地运行查询
location: { $near: { $geometry: loggedin.location } },
But got the same error. 但是得到了同样的错误。
Another interesting fact is if I drop into the native client with mongoose.models.Users.collection
and execute my query I don't receive the error at all. 另一个有趣的事实是,如果我使用
mongoose.models.Users.collection
进入本机客户端并执行查询,那么我根本不会收到错误。
Sample gist with mongo shell output and native query 具有mongo shell输出和本机查询的示例要点
Try out through this code 试试看这段代码
$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.