mongoose聚合$match不匹配

mongoose聚合$match不匹配,mongoose,Mongoose,我有一个对象数组,其中每个对象包含一个动作对象数组和一个子对象。我想对一个特定的子id对象的操作点求和 当我在mongoplayground测试sum时,它的工作原理与预期一样,但在代码中,它不是很好。 请有人帮我指出我遗漏了什么 下面是我的文件: 阵列 [ { "_id": "5e6990148c3e01001388b678", "child": { "image": "", "_id": "5e695fe

我有一个对象数组,其中每个对象包含一个动作对象数组和一个子对象。我想对一个特定的子id对象的操作点求和

当我在mongoplayground测试sum时,它的工作原理与预期一样,但在代码中,它不是很好。 请有人帮我指出我遗漏了什么

下面是我的文件:

阵列

[
    {
        "_id": "5e6990148c3e01001388b678",
        "child": {
            "image": "",
            "_id": "5e695fe97bd186001526798d",
            "name": "Mariana",
            "gender": "female",
            "dateBirth": "12/07/2010",
            "createdAt": "2020-03-11T22:02:17.413Z",
            "updatedAt": "2020-03-11T22:02:17.413Z",
            "__v": 0
        },
        "actions": [
            {
                "_id": "5e6990148c3e01001388b679",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:27:48.747Z",
                "updatedAt": "2020-03-12T01:27:48.747Z"
            },
            {
                "_id": "5e6990148c3e01001388b67a",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:27:48.747Z",
                "updatedAt": "2020-03-12T01:27:48.747Z"
            },
            {
                "_id": "5e6990148c3e01001388b67b",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:27:48.747Z",
                "updatedAt": "2020-03-12T01:27:48.747Z"
            }
        ],
        "createdAt": "2020-03-12T01:27:48.748Z",
        "updatedAt": "2020-03-12T01:27:48.748Z",
        "__v": 0
    },
    {
        "_id": "5e6990578c3e01001388b67c",
        "child": {
            "image": "",
            "_id": "5e644fecffc3150013eeafba",
            "name": "Sheldon",
            "gender": "male",
            "dateBirth": "25/01/2014",
            "createdAt": "2020-03-08T01:52:44.878Z",
            "updatedAt": "2020-03-08T01:52:44.878Z",
            "__v": 0
        },
        "actions": [
            {
                "_id": "5e6990578c3e01001388b67d",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:28:55.922Z",
                "updatedAt": "2020-03-12T01:28:55.922Z"
            },
            {
                "_id": "5e6990578c3e01001388b67e",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:28:55.922Z",
                "updatedAt": "2020-03-12T01:28:55.922Z"
            },
            {
                "_id": "5e6990578c3e01001388b67f",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:28:55.923Z",
                "updatedAt": "2020-03-12T01:28:55.923Z"
            },
            {
                "_id": "5e6990598c3e01001388b680",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:28:57.428Z",
                "updatedAt": "2020-03-12T01:28:57.428Z"
            },
            {
                "_id": "5e6990598c3e01001388b681",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:28:57.428Z",
                "updatedAt": "2020-03-12T01:28:57.428Z"
            },
            {
                "_id": "5e6990598c3e01001388b682",
                "type": "extraNegativePoint",
                "point": -10,
                "pointType": "red",
                "createdAt": "2020-03-12T01:28:57.428Z",
                "updatedAt": "2020-03-12T01:28:57.428Z"
            }
        ],
        "createdAt": "2020-03-12T01:28:55.923Z",
        "updatedAt": "2020-03-12T01:28:57.431Z",
        "__v": 1
    }
]
模式

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Promise = require('promise')

const actionSchema = new Schema({
    type: {
        type: String,
        required: true
    },
    point: {
        type: Number,
        min: -1001,
        max: 1001,
        required: true
    },
    pointType: {
        type: String,
        required: true
    },
    goal:{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Goals'
    }, 
    penalty: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Penalty'
    },
    award: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Award'
    }
},{
    timestamps: true
})

const realizationSchema = new Schema({
    child: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Child'
    },
    actions: [actionSchema]
},
{
    timestamps: true
});

realizationSchema.statics.fetchTotalPointsPerChild = function(childId) {
    return new Promise((resolve, reject) => {
        this.aggregate([
            {
                $match: {
                    "child._id": childId
                }
            },
            {
                $unwind: "$actions"
            },
            {
                $group: {
                    _id: "$child._id",
                    totalPointsPerChild: {
                        $sum: "$actions.point"
                    }
                }
            }], 

                (err, result) => {
                    if (err) {
                        console.log("Error from search: fetchTotalPointsPerChild", err)
                        return reject(err);
                    }
                    resolve(result)
                }
            )
            });
       }
module.exports = mongoose.model('Realization', realizationSchema);
路由器


您试图在错误的示例文档上进行聚合,因为基于模式,action字段只是一个ObjectId,而不是一个对象。它应该是简单的
“child”:ObjectId(“5E644FECFC3150013EEAFBA”)

因此,您的聚合必须如下所示:

realizationSchema.statics.fetchTotalPointsPerChild=函数(childId){
返回新承诺((解决、拒绝)=>{
这个是聚合的(
[
{
$match:{
child:mongoose.Types.ObjectId(childId)//不是“child.\u id”:childId
}
},
{
$unwind:“$actions”
},
{
$group:{
_id:$child',//非_id:$child._id”,
TotalPointsParchild:{
$sum:“$actions.point”
}
}
}
],
(错误,结果)=>{
如果(错误){
log(“搜索错误:fetchTotalPointsPerChild”,err);
退货拒绝(err);
}
控制台日志(结果);
决心(结果);
}
);
});
};
console.log(结果)
将在控制台中显示:

[ { _id: 5e644fecffc3150013eeafba, totalPointsPerChild: -60 } ]

感谢您的回复!除此之外,我不需要将req.params.childId字符串化。
[ { _id: 5e644fecffc3150013eeafba, totalPointsPerChild: -60 } ]