问题描述
我有两个 Document 集合,
- Products => productID,addressId (有地址表的 FK )
- addresses => addressId,Location {type:"point",cordinates : [72.01,32.94] }
现在我正在选择离我最近且最大距离为 25KM 的产品
var aggQuery = [];
if (query.isAddressFilter || (!_.isNaN(parseFloat(query.long)) && !_.isNaN(parseFloat(query.lat))))
{
let addressQuery = {};
addressQuery['$lookup'] = {};
addressQuery['$lookup']['from'] = "addresses";
addressQuery['$lookup']['as'] = "address";
var nearPosition = {
'type': "Point",'coordinates': [parseFloat(query.long),parseFloat(query.lat)]
};
addressQuery['$lookup']['pipeline'] = [{
$geoNear: {
includeLocs: "location",distanceField: "distance",near: nearPosition,maxdistance: 1000 * parseFloat(query.distance),//(KM)
distanceMultiplier: 0.001,spherical: true,}
},{ "$sort": { "distance": 1 } },];
aggQuery.push(addressQuery);
}
var items = await this.productModel.aggregate(aggQuery);
return item ;
根据上面的查询,我得到了结果,但我在 product 下得到了多个地址。
我的期望是:- 获取符合此条件的产品
请建议我怎么做。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)