我在我的Node.js服务器中编写了以下模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var config = require('../../config.js');
var schema = new Schema({
title : { type: String, default: "generic"},
guide : String,
loc : {
type : {type: String, default:"Point"},
coordinates : [ ]
},
}
);
schema.index({"loc":"2dsphere"});
module.exports = mongoose.model(config.DATA_TYPE.SUPPORT, schema);
接下来我编写了用于添加、删除和查询的调度程序。添加和删除都没有问题,但是查询有些问题。
这是我的路由:
router.route('/')
.post(function(req, res, next){
SupportManager.getGuides(req.body.lat, req.body.lon, function(err,data){
if(err){
return res.json({
success: false,
message: 756
});
}else{
return res.json({
success: true,
message: 856,
supports: data
});
}
});
})
我的SupportManager.getGuides
代码如下:
getGuides: function(lat,lon,callback){
Support.find({ loc: { $geoWithin: { $centerSphere: [[ lon , lat], 10/3963.2]}}}, callback);
},
奇怪的现象是我在数据库中添加了以下对象:
{
"_id": "58bf2f07d5fd2a0cdde4cca9",
"guide": "a1",
"loc": {
"coordinates": [
"34",
"22"
],
"type": "Point"
},
"title": "tappp",
"__v": 0
}
但是当我使用lat=22和long=34进行研究时,我得到的答案是:
success: true,
message: 856,
supports: []
数组“supports”为空。
[lon,lat]
。更多信息请参见https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere - s7vr{ loc: { $geoWithin: { $centerSphere: [[ lon , lat], dist ]}}}
- pittuzzo