猫鼬聚合问题

时间:2020-05-30 20:34:36

标签: node.js mongodb mongoose

我正在尝试创建一个API端点,该端点返回状态为被忽略的订单商品的计数。

这是我的模特。

const defectsSchema = new mongoose.Schema({
    "defectId": String,
    "defectLoggedDate": String,
    "custId": String,
    "defectTitle": String,
    "defectDesc": String,
    "defectStatus": String,
    "defectRequestor": String,
    "defectCustRef": String,
    "defectPriority": String,
    "defectCreator": String,
    "defectAssignedOperative": String,
    "defectAssignedOperativeEmail": String,
    "defectAwaitingVar": Boolean,
    "defectRcvdDate": Date,
    "defectDueDate": Date,
    "defectEstimatedDuration": String,
    "defectPlannedStartDate": Date,
    "defectPlannedStartTime": String,
    "defectPlannedEndTime": String,
    "defectCompleteDate": Date,
    "defectVatRate": String,
    "covidOnhold": String,
    "defectTags": [
        {
        "defectTagShortName": String
        }
    ], 
    "defectItems": [
        {
        "defectItemId": String,
        "defectItemCode": String,
        "defectItemLocation": String,
        "defectItemQuantity": String,
        "defectItemUnit": String,
        "defectItemCost": String,
        "defectItemLongDescription": String,
        "defectItemShortDescription": String,
        "defectItemCat": String,
        "defectItemSubCat": String,
        "defectItemRate": String,
        "defectItemStatus": String,
        "defectItemVarFlag": Boolean,
        "defectItemAssignedOperative": String,
        "defectItemCustNotes": String,
        "defectItemDjcsNotes": String
        }
    ], 
    "defectCustAddress1": String,
    "defectCustAddress2": String,
    "defectCustCity": String,
    "defectCustCounty": String,
    "defectCustPostcode": String,
    "defectCustPhone": String,
    "defectCustEmail": String,
    "defectCustName": String,
    "defectCat": String,
    "defectImages": [
        {
            "defectImageUrl": String
        }
    ],
    "defectValue": String,
    "defectNotes": [
        {
            "defectNoteId": String,
            "defectNoteCategory": String,
            "defectNoteReminderEnabled": String,
            "defectNoteStatus": String,
            "defectNoteReminderDate": String,
            "defectNoteText": String,
            "defectNoteAuthor": String,
            "defectNoteCreateDate": Date
        }
    ],
    "defectTasks": [
        {
            "defectTaskStage": String,
            "defectTaskDescription": String,
            "defectTaskStatus": String,
            "defectTaskDetails": String,
            "defectTaskActionDatetime": Date,
            "defectTaskActionExector": String,
            "defectTaskIncompleteDescription": String,
            "defectTaskCompletedDescription": String,
            "defectActiveTask": String,
            "defectTaskAttendDate": Date,
            "defectTaskAttendTime": String,
            "defectTaskNotes001": String,
            "defectTaskNotes002": String,
            "defectTaskNotes003": String,
            "defectTaskNotes004": String,
            "defectTaskAttendReturnVisitDate": Date,
            "defectTaskAttendReturnVisitTime": String,
            "defectAdditionalLabourFlag": String,
            "defectReportSubmitted": String,
            "defectCostsApproved": String 
        }
    ],
    "returnVisits": [
        {
            "returnVisitId": String,
            "returnVisitReason": String
        }
    ]
});

这是我用来尝试返回count和defectId ...

的代码
// Get count of omitted items per order
app.get('/v1/itemsomitted/:id', async(request, response) => {
    try {
        var result = await defectsModel.aggregate([{ $match: { defectId: request.params.id } },
            { $unwind: "$defectItems" },
            { $match: {"defectItems.defectItemStatus": "omitted"} },
            { $group: {
                "_id": {
                    "_id": "$_id",
                    "status": "$defectItems.defectItemStatus"
                },
                "count": { "$sum": 1 }
            }},
            {$project: {defectId: 1, count: 2} }
        ]).exec(function(err, count){
            response.json(count);
        })
    } catch (error) {
        response.status(500).send(error);
    }
});

当我点击API端点时,没有任何响应。我使用的是Mongo Atlas,并使用了Atlas中提供的UI来测试可以在此处正常工作的聚合管道。

有人可以告诉我我在做什么错吗?

0 个答案:

没有答案