【问题标题】:How to use limit, match, sort and groupby in mongo along with toInt containing strings?如何在mongodb中使用limit,match,sort和group by以及包含字符串的poInt?
【发布时间】:2021-07-27 01:28:33
【问题描述】:

我在 mongo 集合中有四个字段 SenderRecipientCreditDebit。其中 Credit 和 Debit 是字符串形式的数字。我需要基于Recipient 进行分组并添加CreditDebit 的整数值。这个结果应该被过滤,因为它包含非零和非负的净额,并根据接收者获得每组的前三名

我有这样的数据库

Sender   Recipient  Credit  Debit
“Alis”   “Mike”       “6”    “-1” 
“Ajay”   “Mike”      “45”    “-31” 
“Arun”   “Mike”      “23”    “-7” 
“Arvind” “Mike”      “45”    “-41”  
“Alis”   “Mary”      “10”    “-11” 
“Ajay”   “Mary”      “10”    “-11” 
“Arun”   “Mary”      “20”    “-31” 
“Arvind” “Mary”      “10”    “-11”     
“Alis”   “Abi”       “16”    “-10” 
“Ajay”   “Abi”       “11”     “0” 
“Arun”   “Abi”       “16”    “-10” 
“Arvind” “Abi”        “6”    “-10”   
“Alis”   “Anu”       “6”     “-15” 
“Ajay”   “Anu”       “16”    “-15” 
“Arun”   “Anu”       “6”     “-15”  
“Arvind” “Anu”       “46”    “-15”   

我需要这样的结果

Sender   Recipient  NetAmount
“Arun”    “Mike”      “16”
"Ajay”    “Mike”      “14”
“Alis”    “Mike”      “5”  
  
“Ajay”    “Abi”      “11”
"Alis”    “Abi”      “6”
“Arun”    “Abi”      “6” 

“Arvind”  “Anu”      “31”
"Ajay”    “Anu”      “11”

我试过这样做

db.collection.aggregate([
  {
    $group: {
      _id: {
        Recipient: "$Recipient"
      },
      Credit: { $sum: { $toInt: "$Credit" } },
      Debit: { $sum: { $toInt: "$Debit" } }
    }
  },
  {
    $project: {
      _id: 0,
      Sender: "$_id.Sender",
      Recipient: "$_id.Recipient",
      NetAmount: { $add: ["$Credit", "$Debit"] }
    }
  },
  { $match: { NetAmount: { $gt: 0 } } },
  { $sort: { NetAmount: -1 } },
  { $limit: 3 }
])

但它没有给出预期的结果。

【问题讨论】:

  • 粘贴你在查询后得到的回复,我看到了一个类似的问题,是你发的吗?
  • 您的问题不清楚,我认为它工作得很好,请参阅playground,您可以添加您的示例文档并确认。
  • @turivishal 我已经更新了问题和示例。希望它可以帮助您解决查询。如果还不清楚,请告诉我
  • @Gandalf 是的,但我需要更精确的工作。这就是为什么我提出了一个新问题。请检查这个问题并帮助我解决
  • @Balaji 能否提供json格式的文件。

标签: node.js mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:
  • $groupsenderrecipient,并计算NetAmount
  • $gt 让 NetAmount 大于零
  • $sort NetAmount 按降序排列
  • $group by recipient only and and 制作文档数组
  • $addFields 从 docs 数组中切片 3 个文档
  • $unwind解构Docs数组
  • $project 显示必填字段
db.collection.aggregate([
  {
    $group: {
      _id: {
        sender: "$sender",
        recipient: "$recipient"
      },
      NetAmount: {
        $sum: {
          $sum: [{ $toInt: "$credit" }, { $toInt: "$debit" }]
        }
      }
    }
  },
  { $match: { NetAmount: { $gt: 0 } } },
  { $sort: { NetAmount: -1 } },
  {
    $group: {
      _id: "$_id.recipient",
      Docs: { $push: "$$ROOT" }
    }
  },
  { $addFields: { Docs: { $slice: ["$Docs", 3] } } },
  { $unwind: "$Docs" },
  {
    $project: {
      _id: 0,
      sender: "$Docs._id.sender",
      recipient: "$Docs._id.recipient",
      NetAmount: "$Docs.NetAmount"
    }
  }
])

Playground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 2023-03-16
    • 2017-08-22
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多