簡體   English   中英

MongoDB 聚合:object 鍵中的元素計數

[英]MongoDB aggregate: count of elements in an object key

我有一個像這樣的 MongoDB 文檔:

[
  {
    "_id": {
      "$oid": "5ff09030cd55d6d9f378d460"
    },
    "username": "a value",
    "userid": "123456",
    "last_access_ts": 1612426253,
    "last_access": "2021-2-4 9:10:53",
    "anotherid": 12345678910,
    "verified_date": "2021-1-2 16:24:32",
    "verified_ts": 1609601072,
    "group_users": {
      "-1001159747589": [
        {
          "anotherid": 12345678910,
          "userid": "123456"
        }
      ],
      "-1001143137644": [
        {
          "anotherid": 12345678910,
          "userid": "123456"
        }
      ],
      "-1001368608972": [
        {
          "anotherid": 12345678910,
          "userid": "123456"
        }
      ]
    },
    "registered_access": "2021-1-2 16:24:42",
  }
]

我有兩個問題。

第一個:我需要計算每個group_users[key] object 中的元素,我堅持使用這個聚合:

db.collection.aggregate([
  {
    $match: {
      username: "a value"
    }
  },
  {
    $project: {
      _id: 1,
      userid: 1,
      "groups": {
        "$objectToArray": "$group_users"
      }
    }
  },
  {
    $unwind: "$groups",
  },
])

這個聚合給了我這個結果:

[
  {
    "_id": ObjectId("5ff09030cd55d6d9f378d460"),
    "groups": {
      "k": "-1001449720492",
      "v": [
        {
          "anotherid": 12345678910,
          "userid": "123456"
        }
      ]
    },
    "userid": "123456"
  },
  {
    "_id": ObjectId("5ff09030cd55d6d9f378d460"),
    "groups": {
      "k": "-1001159747589",
      "v": [
        {
          "anotherid": 12345678910,
          "userid": "123456"
        }
      ]
    },
    "userid": "123456"
  },
  {
    "_id": ObjectId("5ff09030cd55d6d9f378d460"),
    "groups": {
      "k": "-1001143137644",
      "v": [
        {
          "anotherid": 12345678910,
          "userid": "123456"
        }
      ]
    },
    "userid": "123456"
  }
]

如何計算每個groups[v]然后重新分組數據? 我希望得到如下結果:

{
    ... some user data
    "groups": {
        "group_key": "count",
        "second_group_key": "count",
        "third_group_key": "count"
    }

}

是否可以聚合或我需要循環代碼?

我的第二個問題總是關於group_users 是否可以遞歸地在group_users object 中擁有用戶數據?

我的意思是,group_users 中的每個group_users都是一個用戶數組; 從這個數組中,我可以使用userid字段或anotherid字段獲取用戶數據(可能使用$graphLookup嗎?)?

作為第二個聚合的結果,我想要這樣的東西:

{
    ... some user data
    "groups": {
        "group_key": [{"userid": userid, "username": username}],
        "second_group_key": [{"userid": userid, "username": username}],
        "third_group_key": [{"userid": userid, "username": username}]
    }
}

顯然,我可以將這種“遞歸”限制為每次 10 個元素。

感謝您的任何建議。

  • $objectToArray objectToArray 將group_users object 轉換為數組
  • $let綁定變量group_arr用於指定表達式,並返回表達式的結果。
  • $map迭代綁定變量group_arr的循環,獲取 v 的總元素的大小並返回 k 和 v,
  • $arrayToObject將返回的數組從$map轉換為 object
db.collection.aggregate([
  { $match: { username: "a value" } },
  {
    $project: {
      _id: 1,
      userid: 1,
      groups: {
        $let: {
          vars: { group_arr: { $objectToArray: "$group_users" } },
          in: {
            $arrayToObject: {
              $map: {
                input: "$$group_arr",
                in: {
                  k: "$$this.k",
                  v: { $size: "$$this.v" }
                }
              }
            }
          }
        }
      }
    }
  }
])

操場


第二個問題,

  • $unwind解構groups數組
  • $lookup與管道,匹配anotherid $in條件並返回必填字段
  • $group by _id並重建groups數組
  • $arrayToObjectgroups數組轉換為 object
db.collection.aggregate([
  { $match: { username: "a value" } },
  {
    $project: {
      _id: 1,
      userid: 1,
      groups: { $objectToArray: "$group_users" }
    }
  },
  { $unwind: "$groups" },
  {
    $lookup: {
      from: "collection",
      let: { anotherid: "$groups.v.anotherid" },
      pipeline: [
        { $match: { $expr: { $in: ["$anotherid", "$$anotherid"] } } },
        {
          $project: {
            _id: 0,
            userid: 1,
            username: 1
          }
        }
      ],
      as: "groups.v"
    }
  },
  {
    $group: {
      _id: "$_id",
      groups: { $push: "$groups" },
      userid: { $first: "$userid" }
    }
  },
  { $addFields: { groups: { $arrayToObject: "$groups" } } }
])

操場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM