Mongo query aggregate with a conditional group and another field

后端 未结 1 1951
我在风中等你
我在风中等你 2021-01-25 03:14

I followed this post by @Blakes-Seven Mongodb aggregate $group for multiple date ranges

But I need to add an additional group by field \"$User.Account\" and I keep gett

1条回答
  •  温柔的废话
    2021-01-25 03:47

    You can follow below syntax to add another field in $group expression

    db.logs.aggregate([
      { "$match": {
        "DateTime": { "$gte": ninetyDays },
        "Orgname": /Inc/
      }},
      { "$group": {
        "_id": {
          "User": "$User.Account",
          "date": {
            "$cond": [
              { "$lt": [ "$DateTime", sixtyDays ] },
              "61-90",
              { "$cond": [
                { "$lt": [ "$DateTime", thirtyDays ] },
                "31-60",
                "01-30"
              ]}
            ]
          }
        },
        "count": { "$sum": 1 },
      }},
      { "$sort": { "count": -1 }},
      { "$limit": 25}
    ])
    

    0 讨论(0)
提交回复
热议问题