使用 $geonear 和子文档表的 Mongo 聚合

问题描述

我有两个 Document 集合,

  1. Products => productID,addressId (有地址表的 FK )
  2. 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...