我有 店铺 和 产品 两个表
店铺有坐标,然后我根据坐标geoNear排序附近店铺没问题
db.collection('shop').aggregate() .geoNear({ distanceField: 'distance', // 输出的每个记录中 distance 即是与给定点的距离 spherical: true, near: db.Geo.Point(113.3089506, 23.0968251), key: 'location', // 若只有 location 一个地理位置索引的字段,则不需填 includeLocs: 'location', // 若只有 location 一个是地理位置,则不需填 }) .end() .then(res => console.log(res)) .catch(err => console.error(err)) |
但我根据经纬度坐标排序查产品时,就实现不了了,
因为产品没经纬度,经纬度在店铺那,跟着店铺的。
所以要先联表查询店铺,根据店铺的坐标,再来排序.
但geoNear()放方法要求一定要房aggregate(),之后第一,
不能放在lookup()后,会报错啊
要怎么解决啊?
db.collection('productions').aggregate() .lookup({ from: 'shop', localField: 'shopId', foreignField: '_id', as: 'shop', }) .replaceRoot({ newRoot: $.mergeObjects([$.arrayElemAt(['$shop', 0]), '$$ROOT']) }) .geoNear({ distanceField: 'distance', // 输出的每个记录中 distance 即是与给定点的距离 spherical: true, near: db.Geo.Point(113.3089506, 23.0968251), key: 'location', // 若只有 location 一个地理位置索引的字段,则不需填 includeLocs: 'location', // 若只有 location 一个是地理位置,则不需填 }) .end() .then(res => console.log(res)) .catch(err => console.error(err)) |
