mongoose 计算 点距离是多少?请教
发布于 10 个月前 作者 Sxy97 669 次浏览 来自 问答
const Schema = new Schema({
    address: {type: String},
    loc: {
        type: [Number],
        index: '2dsphere'
    }
})
const test = db_conn.model('test', Schema);
test.find({
    loc: {
        $nearSphere: {
            $geometry: {
                type : "Point",
                coordinates : [116.401394, 39.917591]
            }
        }
    }
}).limit(5).exec(function (err, result) {
    console.log(result);
})

//查询结果
[ { _id: 5bab4b7dcdd99b12f00c4aae,
    address: '天安门',
    loc: [ 116.40355, 39.916152 ],
    __v: 0 },
  { _id: 5bab4b451ffff91e309800ff,
    address: '大兴',
    loc: [ 117.144185, 39.650336 ],
    __v: 0 },
  { _id: 5bab4c07370cd024887b407b,
    address: '保定99',
    loc: [ 115.48755, 38.885208 ],
    __v: 0 },
  { _id: 5bab4c07370cd024887b407a,
    address: '保定98',
    loc: [ 115.48754, 38.885197999999995 ],
    __v: 0 },
  { _id: 5bab4c07370cd024887b4079,
    address: '保定97',
    loc: [ 115.48752999999999, 38.885188 ],
    __v: 0 } ]

回到顶部