gpt4 book ai didi

node.js - Mongoose 聚合返回空结果

转载 作者:可可西里 更新时间:2023-11-01 09:08:17 26 4
gpt4 key购买 nike

<分区>

我对 mongoose 聚合请求有疑问。

这有点让我发疯,因为我无法在任何地方找到解决方案。如果有任何支持,我将不胜感激。

架构:

var EvalSchema = new Schema({
modified: {type: Date, default: Date.now},
created : {type: Date, default: Date.now},
username: {type: String, required: true},
item: {type: String, required: true},
criteria: [{
description: {
type: String
},
eval: {
type: Number
}
}]
});
mongoose.model('Eval', EvalSchema);

我正在使用聚合来计算给定项目的每个标准的评估总和。

Eval.aggregate([{
$match: {
item: item.id
}
}, {
$unwind: "$criteria"
}, {
$group: {
_id: "$criteria.description",
total: {
$sum: "$criteria.eval"
},
count: {
$sum: 1
}
}
}, {
$project: {
total: 1,
count: 1,
value: {
$divide: ["$total", "$count"]
}
}
}], function(err, result) {
if (err) {
console.log(err);
}
console.log(result);
});

结果总是空的....

我正在记录 mongoose 在应用程序中触发的所有查询。当我在 Mongodb 中运行查询时,它返回了正确的结果。

coll.aggregate([{
'$match': {
item: 'kkkkkkkkkkk'
}
}, {
'$unwind': '$criteria'
}, {
'$group': {
_id: '$criteria.description',
total: {
'$sum': '$criteria.eval'
},
count: {
'$sum': 1
}
}
}, {
'$project': {
total: 1,
count: 1,
value: {
'$divide': ['$total', '$count']
}
}
}])

结果:

{
result: [{
"_id": "Overall satisfaction",
"total": 4,
"count": 1,
"value": 4
}, {
"_id": "service",
"total": 3,
"count": 1,
"value": 3
}, {
"_id": "Quality",
"total": 2,
"count": 1,
"value": 2
}, {
"_id": "Price",
"total": 1,
"count": 1,
"value": 1
}],
ok: 1
}

模型引用了正确的集合。

谢谢你:)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com