简体   繁体   English

MongoDB 查询仅显示大于平均值的数据

[英]MongoDB Query to Show only data with greater than average

DEMO https://mongoplayground.net/p/ImiJWm1s-lx演示https://mongoplayground.net/p/ImiJWm1s-lx

db={
  BRANCH: [
    {
      "BCODE": "B1",
      "BNAME": "CANADA"
    },
    {
      "BCODE": "B2",
      "BNAME": "USA"
    },
    {
      "BCODE": "B3",
      "BNAME": "CANADA"
    }
  ],
  ACCOUNT: [
    {
      "ANO": 1,
      "Amount": 1000,
      "BCODE": "B1"
    },
    {
      "ANO": 2,
      "Amount": 350,
      "BCODE": "B1"
    },
    {
      "ANO": 3,
      "Amount": 450,
      "BCODE": "B1"
    },
    {
      "ANO": 4,
      "Amount": 360,
      "BCODE": "B2"
    },
    {
      "ANO": 5,
      "Amount": 800,
      "BCODE": "B2"
    },
    {
      "ANO": 6,
      "Amount": 450,
      "BCODE": "B3"
    },
    {
      "ANO": 7,
      "Amount": 360,
      "BCODE": "B2"
    },
    {
      "ANO": 8,
      "Amount": 800,
      "BCODE": "B1"
    }
  ]
}

I have this MongoDB collection.我有这个 MongoDB 集合。 I am able to show no of accounts in each branch Using This Query :我可以使用此查询在每个分支中显示没有帐户:

db.BRANCH.aggregate([
  {
    $lookup: {
      from: "ACCOUNT",
      localField: "BCODE",
      foreignField: "BCODE",
      as: "TOTAL_ACCOUNTS"
    }
  },
  {
    "$addFields": {
      "TOTAL ACCOUNTS": {
        $size: "$TOTAL_ACCOUNTS"
      }
    }
  },
  {
    "$project": {
      _id: 0,
      TOTAL_ACCOUNTS: 0
    }
  }
])

OUTPUT输出

[
  {
    "BCODE": "B1",
    "BNAME": "CANADA",
    "TOTAL ACCOUNTS": 4
  },
  {
    "BCODE": "B2",
    "BNAME": "USA",
    "TOTAL ACCOUNTS": 3
  },
  {
    "BCODE": "B3",
    "BNAME": "CANADA",
    "TOTAL ACCOUNTS": 1
  }
]

but I need to show only those branch details where No. of accounts is more than an average number of accounts in all branches.但我只需要显示那些账户数量超过所有分支机构平均账户数量的分支机构详细信息。

So according to the given data AVG Accounts of all branches is (4+3+1)/3 = 2所以根据给定的数据,所有分行的 AVG Accounts 是 (4+3+1)/3 = 2

It means Only Branch B1 & Branch B2 should be displayed because they have 4 accounts and 3 accounts respectively.这意味着只应显示 Branch B1 和 Branch B2,因为它们分别有 4 个帐户和 3 个帐户。

Branch B3 should not be displayed because It has only 1 Account & 1<2 (Avg No of account).不应显示分支 B3,因为它只有 1 个帐户 & 1<2(帐户的平均数)。

What should i use here?我应该在这里使用什么?

  • $group by null and construct the array of branches and get the average of the total size of accounts $group by null 并构造branches数组并获得帐户总大小的平均值
  • $filter to iterate loop of branches array and check condition that total should greater than average of the total size of accounts $filter迭代branches数组的循环并检查总数应大于帐户总大小的平均值的条件
db.BRANCH.aggregate([
  {
    $lookup: {
      from: "ACCOUNT",
      localField: "BCODE",
      foreignField: "BCODE",
      as: "TOTAL_ACCOUNTS"
    }
  },
  { $addFields: { TOTAL_ACCOUNTS: { $size: "$TOTAL_ACCOUNTS" } } },
  {
    $group: {
      _id: null,
      branches: { $push: "$$ROOT" },
      totalAvg: { $avg: "$TOTAL_ACCOUNTS" }
    }
  },
  {
    $project: {
      _id: 0,
      branches: {
        $filter: {
          input: "$branches",
          cond: { $gt: ["$$this.TOTAL_ACCOUNTS", "$totalAvg"] }
        }
      }
    }
  }
])

Playground操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM