Mongodb geoNear 和组聚合

Posted

技术标签:

【中文标题】Mongodb geoNear 和组聚合【英文标题】:Mongodb geoNear and group aggregation 【发布时间】:2015-09-15 21:19:44 【问题描述】:

我一直在网上搜索与此相关的内容,但找不到。

我有这个聚合

 Place.aggregate(
    [
         "$geoNear": 
            "near": 
                "type": "Point",
                "coordinates": [longitude, latitude]
            ,
            "spherical": true,
            "distanceField": "distance"
        ,
         $group:
           _id: "$_id",
            name:  '$first': '$name' ,
            distance:  $first: "$distance" 
        
        ,
         $project : 
            name: 1,
            distance: 1,
        
    ],
    function(error, places) 
        if (error) return callback(error, null);
        callback(null, places)
    
);

它可以工作,但是 geoNear 排序丢失了!

但这给了我正确排序的文档:

    Place.aggregate(
    [
         "$geoNear": 
            "near": 
                "type": "Point",
                "coordinates": [longitude, latitude]
            ,
            "spherical": true,
            "distanceField": "distance"
        
    ],
    function(error, places) 
        if (error) return callback(error, null);
        callback(null, places)
    
);

有什么想法吗?

为了让您了解我在这里尝试做什么是我使用的完整查询

    Place.aggregate(
    [
         "$geoNear": 
            "near": 
                "type": "Point",
                "coordinates": [longitude, latitude]
            ,
            "spherical": true,
            "distanceField": "distance"
        ,
        "$unwind": "$participants"  ,
         $group:
           _id: "$_id",
            name:  '$first': '$name' ,
            distance:  $first: "$distance" ,
            person:  $sum: 1 ,
            sumof: $sum: "$participants.age"
        
        ,
         $project : 
            name: name,
            distance: 1,
            person: 1,
            meanAge: $divide: [ "$sumof", "$person" ]
        
    ],
    function(error, places) 
        if (error) return callback(error, null);
        callback(null, places)
    
);

【问题讨论】:

您正在对主键“_id”进行分组,这实际上并没有做任何事情。为什么要这么做?您还想在这里做什么? 我显示的代码被简化了,我的目标是获取具有按 geoNear 排序的自定义字段(使用组和项目)的文档。 我编辑了我的问题,应该更清楚我现在想要做什么。 【参考方案1】:

简而言之,当您使用$group 等运算符时,无法保证返回结果的顺序。文档将按照“输入”到组管道的顺序进行处理,以兑现 $first 之类的内容,但输出不一定与输入的顺序相同。

直接来自文档:

$group 不会对其输出文档进行排序。

实际上,您可能会发现顺序是按分组键但在大多数情况下是相反的。

如果你想有一个特定的输出顺序,那么使用$sort 并且对于最终的“输出”应该是你的最后一个管道阶段,所以没有其他东西会改变这个顺序。

Place.aggregate(
    [
         "$geoNear": 
            "near": 
                "type": "Point",
                "coordinates": [longitude, latitude]
            ,
            "spherical": true,
            "distanceField": "distance"
        ,
         "$unwind": "$participants"  ,
         "$group":    
            "_id": "$_id",
             "name":  "$first": "$name" ,
             "distance":  "$first": "$distance" ,
             "person":  "$sum": 1 ,
             "sumof": "$sum": "$participants.age" 
        ,
         "$project" : 
            "name": 1,
            "distance": 1,
            "person": 1,
            "meanAge":  "$divide": [ "$sumof", "$person" ]
        ,
         "$sort":  "distance": 1  
    ],
    callback
 );

【讨论】:

以上是关于Mongodb geoNear 和组聚合的主要内容,如果未能解决你的问题,请参考以下文章

GraphQL和MongoDB` $ geoNear`聚合

MongoDB $geoNear 聚合管道(使用查询选项和使用 $match 管道操作)给出不同的结果

MongoDB GeoNear 聚合

如何对嵌入式文档进行 $geoNear 聚合?

添加附加查询时,Doctrine MongoDB geoNear() 参数设置为 0

我可以通过 ruby​​ 驱动程序访问 mongo 的 geoNear 功能吗?