我寫了下面的模式在我的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]'在您的centerSphere座標中。更多在這裏https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere – Veeram
我修好了,但仍然沒有工作... – pittuzzo
不完全確定。你是否也修復了文件? – Veeram