代码之家  ›  专栏  ›  技术社区  ›  avrono

聚合和分组的氧化镁

  •  0
  • avrono  · 技术社区  · 9 年前

    为了有效地从连接中获得不同的值,我知道这可能不是在Mongo中使用的最佳范例。

    pipe := []bson.M{
    
        {
            "$group": bson.M{
                "_id":  bson.M{"user": "$user"},
    
            },
        },
    
        {
            "$match": bson.M{
                "_id":  bson.M{"$exists": 1},
                "user": bson.M{"$exists": 1},
                "date_updated": bson.M{
                    "$gt": durationDays,
                },
            },
    
        },
    
        {
            "$lookup": bson.M{
                "from":         "users",
                "localField":   "user",
                "foreignField": "_id",
                "as":           "user_details",
            },
        },
        {
            "$lookup": bson.M{
                "from":         "organizations",
                "localField":   "organization",
                "foreignField": "_id",
                "as":           "organization_details",
            },
        },
    
    }
    
    err := d.Pipe(pipe).All(&result)
    

    $group 节中,查询按预期返回连接。

    如果我按原样跑步,我会 NULL

    如果我移动 $组 在管道的底部,我得到了一个带有Null值的数组响应

    是否可以使用 $组 (目的是模拟 DISTINCT ) ?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Wan B.    8 年前

    你得到NULL的原因是因为你的 $match $group 阶段

    $组

      {"_id": { "user": "foo"}},
      {"_id": { "user": "bar"}},
      {"_id": { "user": "baz"}}
    

    它们不再包含其他字段,即。 user , date_updated 和 organization Group Accumulator Operator Aggregation Expression Variables

    例如,使用 mongo shell ,让我们使用 $first operator 这基本上是第一次出现。这可能对 但不是为了 更新日期 . 请选择更合适的累加器运算符。

    {"$group": { 
              "_id":"$user", 
              "date_updated": {"$first":"$date_updated"}, 
              "organization": {"$first":"$organization"}
             }
    }
    

    {"_id":{"user":"$user"}} 使用simpler {"_id":"$user"} .

    $project stage 重命名我们的结果 _id 字段从组操作返回到 . 也可以携带其他字段而不进行修改。

    {"$project": {
                  "user": "$_id", 
                  "date_updated": 1, 
                  "organization": 1
                 }
     }
    

    你的 $match stage 只需列出 更新日期 滤器首先我们可以删除 _id 因为到目前为止,它在管道中不再相关,而且如果您想确保只处理具有 使用者 您应该重视的价值 $匹配 在 $组 Aggregation Pipeline Optimization

    因此,所有这些组合将如下所示:

    [
     {"$group":{ 
                 "_id": "$user", 
                 "date_updated": { "$first": "$date_updated"}, 
                 "organization": { $first: "$organization"} 
               }
     },
     {"$project":{ 
                   "user": "$_id", 
                   "date_updated": 1, 
                   "organization": 1
                 }
     }, 
     {"$match":{
              "date_updated": {"$gt": durationDays } }
     }, 
     {"$lookup":{
                 "from": "users", 
                 "localField": "user", 
                 "foreignField": "_id", 
                 "as": "user_details"
                }
     }, 
     {"$lookup":{
                "from": "organizations", 
                "localField": "organization", 
                "foreignField": "_id", 
                "as": "organization_details"
                }
     }
    ]
    

    users 和 organizations 集合,根据您的应用程序用例,您可以重新考虑嵌入一些值。你可能会发现 6 Rules of Thumb for MongoDB Schema Design 有用的

    推荐文章