如何在mongodb中查找后对sum进行分组?

时间:2017-06-06 12:20:35

标签: node.js mongodb aggregation-framework

db.transections.aggregate(
    [

        {
            $lookup: {
                from: "activitylogs",
                localField: "transectionid",
                foreignField: "ref_id",
                as: "point"
            }
        },

        {
            $group: {
                _id: {
                    mobilenumber: "mobilenumber"
                },
                amount: {
                    $sum: "transectionamount"
                },
                points: {
                    $sum: "point.givenpoint"
                },
                count: {
                    $sum: 1
                }
            }
        }

    ]
)

这里我试图得到transectionamount和givenpoint的总和,但是当我运行这个查询时,这并没有给出任何结果。

mobilenumber和transectionamount可用于transections集合

activitylogs集合中提供的给定点

我如何查询获得transectionamount和givenpoint的总和任何人帮助我 我的TransectionSchema在下面给出了

var TransectionSchema = new Schema({
  transectiondate:Date,
  created:String,
  transectionid :String,
  transectionamount: SchemaTypes.Double,
  mobilenumber:String   

});

我的ActivitylogSchema

var ActivitylogSchema = new Schema({
    ref_id: String,
    mobilenumber:String,
    givenpoint: SchemaTypes.Double

});

0 个答案:

没有答案