【问题标题】:Group count 2 item object array mongodb组数 2 项对象数组 mongodb
【发布时间】:2019-02-10 00:57:28
【问题描述】:

我有以下数组:

[
  {
    “counter”: 123456,
    “user”: “USER1”,
    “last”: “USER1”
  },
  {
    “counter”: 123,
    “user”: “USER1”,
    “last”: “USER2”
  },
  {
    “counter”: 111,
    “user”: “USER2”,
    “last”: “USER2”
  },
  {
    “counter”: 1122,
    “user”: “USER2”,
    “last”: “USER2”
  },
  {
    “counter”: 112233,
    “user”: “USER1”,
    “last”: “USER2”
  },
]

我在 mongodb 上做如下查询:

{$group: {
  _id: “$user”,
  total: {$sum: 1},
  last: {$sum: {$cond: [
    {$eq: ['$last’, '$user']}, 1, 0
  ]}}
}}

我想得到以下结果:

[
  {
    _id: “USER1”
    total: 3,
    last: 1
  },
{
    _id: “USER2”
    total: 2,
    last: 4
  }
]

但我明白了:

[
  {
    _id: “USER1”
    total: 3,
    last: 1
  },
{
    _id: “USER2”
    total: 2,
    last: 2
  }
]

当我分组时,我无法满意地计算最后一项

我怎样才能得到预期的结果?感谢您的帮助。

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    您只有 2 个“USER2”,然后不能在该 _id 上 $sum 4 $last。 如果您需要用户中的索引,请尝试使用 compose _id。

    db.teste.aggregate([
      {$group:{
        _id: {user:"$user",last:"$last"},
        total: {$sum:1},
        last: {
          $sum: {
            $cond:[ {$eq: ["$last", user]}, 1, 0]
          }
        }
      }}
    ])
    

    如果您需要在不同的字段中添加字符串或其他匹配项。查询是:

    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }},
      {$lookup:{
        from: "teste",
        let: { indice: "$_id" },
        pipeline: [
          {$group:{
            _id: null,
            user:{$sum:{$cond:[
              {$eq:["$user", "$$indice"]}, 1, 0
            ]}},
            last:{$sum:{$cond:[
              {$eq:["$last", "$$indice"]}, 1, 0
            ]}}
          }},
          {$project:{
            _id: 0
          }}
        ],
        as: "res"
      }}
    ])
    

    从零到英雄:

    //First we'll extract only what we need find, on this case distinct users
    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }}
    ])
    
    //Here we will get the indexes of the search and reinsert in our filter using $let (nodejs var)
    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }},
      {$lookup:{
        from: "teste",
        let: { indice: "$_id" },
        pipeline: [],
        as: "res"
      }}
    ])
    
    //Let's counter the total of reinserted elements
    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }},
      {$lookup:{
        from: "teste",
        let: { indice: "$_id" },
        pipeline: [
          {$group:{
            _id: null,
            total:{$sum:1}
          }}
        ],
        as: "res"
      }}
    ])
    
    //Now let's test a true condition
    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }},
      {$lookup:{
        from: "teste",
        let: { indice: "$_id" },
        pipeline: [
          {$group:{
            _id: null,
            user:{$sum:{$cond:[
              {$eq:[true, true]}, 1, 0
            ]}}
          }}
        ],
        as: "res"
      }}
    ])
    
    //cond tested let's extract what we want
    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }},
      {$lookup:{
        from: "teste",
        let: { indice: "$_id" },
        pipeline: [
          {$group:{
            _id: null,
            user:{$sum:{$cond:[
              {$eq:["$user", "$$indice"]}, 1, 0
            ]}},
            last:{$sum:{$cond:[
              {$eq:["$last", "$$indice"]}, 1, 0
            ]}}
          }}
        ],
        as: "res"
      }}
    ])
    
    //Let's take the _id of the sub-colection because we do not need it
    db.teste.aggregate([
      {$group:{
        _id: "$user"
      }},
      {$lookup:{
        from: "teste",
        let: { indice: "$_id" },
        pipeline: [
          {$group:{
            _id: null,
            user:{$sum:{$cond:[
              {$eq:["$user", "$$indice"]}, 1, 0
            ]}},
            last:{$sum:{$cond:[
              {$eq:["$last", "$$indice"]}, 1, 0
            ]}}
          }},
          {$project:{
            _id: 0
          }}
        ],
        as: "res"
      }}
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多