【发布时间】:2022-10-13 06:12:54
【问题描述】:
我希望有人可以帮助我:我想用javascript和猫鼬写一点代码。我的目标是:人们可以使用在线公式登录活动。 他们必须指定姓氏、人数(每组)、电子邮件、时间(时段)。 我想编写一个控制结构来检查一个插槽中是否已经有特定数量的人(例如,插槽 11 时钟中最多 50 人)。 我不知道如何从聚合中返回单个值。
我的代码:
const notesSchema = {
lastname: String,
people: Number,
email: String,
time: Number
}
const Note = mongoose.model("Note", notesSchema)
async function totalamount(){
Note.aggregate(
[{
$group:
{
_id: null,
total: { $sum: "$people"}
}}
])
}
有人能帮助我吗? 抱歉英语不好。
来自德国的问候 戴夫
【问题讨论】:
-
请以mongoose docs about using aggregate 开头。注意
await关键字的使用。一旦你得到结果。您可以使用res[0].total
标签: javascript mongodb mongoose aggregate