I got this query :
exports.search = (req, res) => {
let lat1 = req.body.lat;
let lon1 = req.body.lng;
let page = req.body.page || 1;
let perPage = req.body.perPage || 10;
let radius = req.body.radius || 100000; // This is not causing the issue, i can remove it and the issue is still here
var options = { page: page, limit: perPage, sortBy: { updatedDate: -1 } }
let match = {}
var aggregate = null;
if (lat1 && lon1) {
aggregate = Tutor.aggregate([
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [lon1, lat1]
},
"distanceField": "distance", // this calculated distance will be compared in next section
"distanceMultiplier": 0.001,
"spherical": true,
"key": "loc",
"maxDistance": radius
}
},
{
$match: match
},
{ "$addFields": { "islt": { "$cond": [{ "$lt": ["$distance", "$range"] }, true, false] } } },
{ "$match": { "islt": true } },
{ "$project": { "islt": 0 } }
])
// .allowDiskUse(true);
} else {
aggregate = Tutor.aggregate([
{
$match: match
}
]);
}
Tutor
.aggregatePaginate(aggregate, options, function (err, result, pageCount, count) {
if (err) {
console.log(err)
return res.status(400).send(err);
}
else {
var opts = [
{ path: 'levels', select: 'name' },
{ path: 'subjects', select: 'name' },
{ path: 'assos', select: 'name' }
];
Tutor
.populate(result, opts)
.then(result2 => {
return res.send({
page: page,
perPage: perPage,
pageCount: pageCount,
documentCount: count,
tutors: result2
});
})
.catch(err => {
return res.status(400).send(err);
});
}
})
};
查询应该检索给定范围内(这是教师模型的一个字段,以公里为单位的整数,表示教师愿意移动的距离)的所有教师,并围绕某个位置(lat1, lon1)。问题在于并非所有文档都被返回。经过多次测试,我注意到只有距离位置约7.5公里以下的导师会被返回,而不是其他人。即使导师距离位置10公里,且范围为15公里,也不会被返回,因为它距离7.5公里更远。我已尝试在两个教师之间(一个被返回,另一个不被返回但应该被返回)切换位置,以查看是否仅此导致了问题,结果确实如此。在我交换他们的位置(lng和loc)后,先前被返回的不再被返回,反之亦然。我真的不明白为什么会发生这种情况。此外,我知道结果大小小于16MB,因为即使使用allowDiskUse:true,我也没有得到所有结果。如果您对我无法获得所有结果的原因有任何其他想法,请不要犹豫!谢谢!PS:这是关于教师模型及其相关字段(loc)的一部分:
import mongoose from 'mongoose';
import validate from 'mongoose-validator';
import { User } from './user';
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate';
var ObjectId = mongoose.Schema.Types.ObjectId;
var rangeValidator = [
validate({
validator: (v) => {
v.isInteger && v >= 0 && v <= 100;
},
message: '{VALUE} is a wrong value for range'
})
];
var tutorSchema = mongoose.Schema({
fullName: {
type: String,
trim: true,
minlength: [1, 'Full name can not be empty'],
required: [true, 'Full name is required']
},
location: {
address_components: [
{
long_name: String,
short_name: String,
types: String
}
],
description: String,
lat: Number,
lng: Number
},
loc: {
type: { type: String },
coordinates: []
},
});
tutorSchema.plugin(mongooseAggregatePaginate);
tutorSchema.index({ "loc": "2dsphere" });
var Tutor = User.discriminator('Tutor', tutorSchema);
module.exports = {
Tutor
};
用户模型正在使用两个索引。一个是ID,另一个是这个;
db['system.indexes'].find() Raw Output
{
"v": 2,
"key": {
"loc": "2dsphere"
},
"name": "loc_2dsphere",
"background": true,
"2dsphereIndexVersion": 3,
"ns": "verygoodprof.users"
}
allowDiskUse:true
,它将使用硬盘作为存储。如果使用此选项获得所有结果,则是16MB
的问题。否则,查询存在问题。 - AabidallowDiskUse:true
运行? - Aabid