【问题标题】:compare sum due date with current date by condional in aggregate mongodb?在mongodb中按条件比较总和到期日期与当前日期?
【发布时间】:2018-11-16 08:39:09
【问题描述】:

我对聚合有一些问题,我有一个集合。我有如下代码的集合。依此类推,我有一个现场调用 dueDate 与当前日期 2018-11-16 进行比较。我需要通过比较 当前日期dueDate totalfirstAmount from remaining 比较没有 duDate 和 dueDate 小于或等于 当前日期 和另一秒 totalSecondAmount from remaining 仅找到 dueDate 更大的当前日期

let myCollection=
  {
  "_id" : "001-29", 
  "tranType" : "Bill",
  "tranDate" : ISODate("2018-11-16T14:55:16.621+07:00"),    
  "vendorId" : "001-2", 
  "dueDate" : ISODate("2018-11-17T14:55:16.621+07:00"), 
  "remaining" : 45,
  "branchId" : "001",     
  },

/* 2 */
  {
  "_id" : "001-26", 
  "tranType" : "Bill",
  "tranDate" : ISODate("2018-11-15T14:22:48.138+07:00"),    
  "vendorId" : "001-9", 
  "withdrawal" : 0,
  "remaining" : 90,
  "branchId" : "001",     
  "memo" : null,    
  },

/* 3 */
  {
  "_id" : "001-18", 
  "tranType" : "Bill",
  "tranDate" : ISODate("2018-11-08T14:18:36.543+07:00"),    
  "vendorId" : "001-1", 
  "billDate" : ISODate("2018-11-15T14:18:36.543+07:00"),
  "dueDate" : ISODate("2018-11-15T14:18:36.543+07:00"), 
  "remaining" : 450,      
  "memo" : null
  },

我想要如下结果

"lowerDueTotalRemaining" : 540,
"overDueTotalRemaining" : 45

【问题讨论】:

    标签: javascript mongodb aggregate


    【解决方案1】:

    我们可以使用aggregation pipeline 来得到想要的结果。

    管道阶段$project$group 与管道运算符$sum$cond 一起使用

    在给定的集合中,必须执行一个条件来确定给定的文档是过期还是过期,下面查询的第一部分将文档分为过期和过期。为了我们在下一阶段的分组方便,我为 OverDue 添加了一个标志“O”,为 LowerDue 添加了一个“L”。

    db.collection_name.aggregate([
      {
        $project: {
          summation: {
            $cond: {
              if: {
                $gte: ["$dueDate", new Date("2018-11-16")]
              },
              then: {
                overDueTotalRemaining: { $sum: "$remaining" },
                flag: "O"
              },
              else: {
                lowerDueTotalRemaining: { $sum: "$remaining" },
                flag: "L"
              }
            }
          }
        }
      },
      {
        $group: {
          _id: "$summation.flag",
          lowerDueTotalRemaining: {
            $sum: "$summation.lowerDueTotalRemaining"
          },
          overDueTotalRemaining: {
            $sum: "$summation.overDueTotalRemaining"
          }
        }
      },
      {
        $project: {
          ans: {
            $cond: {
              if: {
                $eq: [ "$_id", "O" ]
              },
              then: {
                "overDueTotalRemaining": "$overDueTotalRemaining"
              },
              else: {
                "lowerDueTotalRemaining": "$lowerDueTotalRemaining"
              }
            }
          }
        }
      }
    ]);
    

    与我们在执行上述查询时获得的一些额外属性匹配的答案如下所示

    { "_id" : "L", "ans" : { "lowerDueTotalRemaining" : 540 } }
    { "_id" : "O", "ans" : { "overDueTotalRemaining" : 45 } }
    

    【讨论】:

    • 对不起,我混淆了,实际上结果是这样的 "lowerDueTotalRemaining" : 135, "overDueTotalRemaining" : 450
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2020-08-12
    • 2013-01-04
    • 1970-01-01
    • 2017-05-15
    • 2011-03-26
    • 1970-01-01
    相关资源
    最近更新 更多