Mongodb Mongo-聚合组查找值

Mongodb Mongo-聚合组查找值,mongodb,mongoose,Mongodb,Mongoose,我正在尝试汇总查找中列出的值。我有两个收藏: 订阅(伪代码) 付款 { _id, statementMonth: String, // e.g. 2020-08 (same as grouped id in Subscriptions) paymentDate: Date, userID: String, amountPaid } 用户需要在月底转移利润率值。因此,我想为语句创建一个输出。这些系统将所有订阅和付款分组到月度记录中,并汇总其中的所有数据。我已经成功地

我正在尝试汇总查找中列出的值。我有两个收藏:

订阅(伪代码)

付款

{
   _id,
   statementMonth: String, // e.g. 2020-08 (same as grouped id in Subscriptions)
   paymentDate: Date,
   userID: String,
   amountPaid
}
用户需要在月底转移利润率值。因此,我想为语句创建一个输出。这些系统将所有订阅和付款分组到月度记录中,并汇总其中的所有数据。我已经成功地创建了第一组的所有内容,但是一旦我进行查找以获取付款详细信息,所有内容似乎都失败了

这是我目前的管道

    {
        "$match": {
            "userID": [provided UserID]
        }
    }, 
    {
        "$group": {
            "_id": {
                "$dateToString": {
                    "format": "%Y-%m",
                    "date": "$purchaseDate"
                }
            },
            "totalWholesalePrice": {
                "$sum": "$wholesalePrice"
            },
            "totalPurchasePrice": {
                "$sum": "$purchasePrice"
            },
            "count": {
                "$sum": 1.0
            }
        }
    }, 
    {
        "$addFields": {
            "totalAmountDue": {
                "$subtract": [
                    "$totalPurchasePrice",
                    "$totalWholesalePrice"
                ]
            }
        }
    }, 
    {
        "$lookup": {
            "from": "transactions",
            "localField": "_id",
            "foreignField": "statementMonth",
            "as": "transactions"
        }
    }, 
    {
        "$unwind": {
            "path": "$transactions",
            "preserveNullAndEmptyArrays": true
        }
    }, 
    {
        "$sort": {
            "_id": -1.0
        }
    }
如果有两个事务,则返回两条记录:

{ 
    "_id" : "2020-08", 
    "totalWholesalePrice" : NumberInt(89), 
    "totalPurchasePrice" : 135.55, 
    "count" : 8.0, 
    "totalAmountDue" : 46.55, 
    "transactions" : {
        "_id" : ObjectId("5f3faf2216d7a517bc51bfae"), 
        "date" : ISODate("2020-04-20T11:23:40.284+0000"), 
        "statementMonth" : "2020-08", 
        "merchant" : "M1268360", 
        "amountPaid" : "40"
    }   
}

{ 
    "_id" : "2020-08", 
    "totalWholesalePrice" : NumberInt(89), 
    "totalPurchasePrice" : 135.55, 
    "count" : 8.0, 
    "totalAmountDue" : 46.55, 
    "transactions" : {
        "_id" : ObjectId("5f3fc13f16d7a517bc51c047"), 
        "date" : ISODate("2020-04-20T11:23:40.284+0000"), 
        "statementMonth" : "2020-08", 
        "merchant" : "M1268360", 
        "amountPaid" : "2"
    }
}
我希望最终JSON是:

    { 
        "_id" : "2020-08", 
        "totalWholesalePrice" : NumberInt(89), 
        "totalPurchasePrice" : 135.55, 
        "count" : 8.0, 
        "totalAmountDue" : 46.55, 
        "transactions" : [{
            "_id" : ObjectId("5f3faf2216d7a517bc51bfae"), 
            "date" : ISODate("2020-04-20T11:23:40.284+0000"), 
            "statementMonth" : "2020-08", 
            "merchant" : "M1268360", 
            "amountPaid" : "40"
        } 
        {
            "_id" : ObjectId("5f3fc13f16d7a517bc51c047"), 
            "date" : ISODate("2020-04-20T11:23:40.284+0000"), 
            "statementMonth" : "2020-08", 
            "merchant" : "M1268360", 
            "amountPaid" : "2"
        }],
        "totalPaid" : 42, 
        "totalBalance" : 4.55, 
    }

您需要再添加一个管道

db.collection.aggregate([
  {
    $group: {
      "_id": "$id",
      "transactions": { //Grouping transactions together
        "$addToSet": "$transactions"
      },
      "totalWholesalePrice": {//As all values are unique getting the first
        "$first": "$totalWholesalePrice"
      }
      //Similarly add the needed fields - use $first, $addToSet
    }
  }
])

要获取总数,请执行以下操作:

db.collection.aggregate([
  {
    $group: {
      "_id": "$id",
      "transactions": {
        "$addToSet": "$transactions"
      },
      "totalWholesalePrice": {
        "$first": "$totalWholesalePrice"
      },
      "totalAmountPaid": {
        $sum: {
          $toInt: "$transactions.amountPaid" //you stored as string, convert to appropriate type
        }
      }
    }
  }
])

感谢Gibbs,这有助于对交易进行分组,但是,我如何获得amountPaid的总值?我尝试了:totalPaid:{$sum:$transactions.amountPaid}。应该是42岁,但我得到了0Gipps-你是一把枪!解决了一个我一整天都想解决的问题。谢谢你的帮助。如果答案有帮助的话,你可以向上投票并接受吗:)+1表示你的尝试和正确的问题已被接受,不幸的是,我的代表太低,无法向上投票。希望这有助于其他人,他们会继续关注以下问题:)
db.collection.aggregate([
  {
    $group: {
      "_id": "$id",
      "transactions": {
        "$addToSet": "$transactions"
      },
      "totalWholesalePrice": {
        "$first": "$totalWholesalePrice"
      },
      "totalAmountPaid": {
        $sum: {
          $toInt: "$transactions.amountPaid" //you stored as string, convert to appropriate type
        }
      }
    }
  }
])