组计数2项对象阵列的mongodb(Group count 2 item object array

2019-09-27 17:29发布

我有以下阵列:

[
  {
    “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
  }
]

当我做了我组不能令人满意算最后一个项目

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

Answer 1:

你只有2“用户2”,则不能$和$ 4持续对_id。 尝试使用,如果你的用户需要通过指数组成_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"
  }}
])


文章来源: Group count 2 item object array mongodb