mongodb - Mongoose/Mongodb Aggregate - 对多个字段进行分组和平均
问题描述
我有一个带有 2 个字段的 Post 模型:日期和评级。我将如何获得每个日期的平均总评分?因此,首先按日期分组,然后在该日期的所有帖子中平均评分。我需要在猫鼬中做到这一点,但他们的文档很难理解。
const PostSchema = new Schema({
date: {
type: String,
default: getToday() //this is just a new Date() formatted
},
rating: {
type: Number,
required: true
}
},
)
这给了我所有日期的平均值,但我不知道如何按日期过滤它:
Post.aggregate([
{ $group: { _id: null, avgRating: { $avg: '$rating' }}}
])
.then(function (res) {
console.log(res[0]["avgRating"]);
})
解决方案
这对我有用:
Post.aggregate([
{ $group: { _id: "$date", avgRating: { $avg: '$rating' }}}
]).
then(function (res) {
console.log(res);
})
输出:
[
{ _id: 'Aug 18, 2021', avgRating: 3.0212234706616727 },
{ _id: 'Aug 19, 2021', avgRating: 2.9680319680319682 },
{ _id: 'Aug 20, 2021', avgRating: 3.023976023976024 },
{ _id: 'Aug 17, 2021', avgRating: 2.9600665557404326 },
{ _id: 'Aug 21, 2021', avgRating: 3.072661217075386 }
]
但是,如果我可以根据其他因素以某种方式过滤它,那就太好了。例如,每个帖子都有一个作者(参考用户模型)。我将如何根据作者的 country.name 或性别进行过滤?
用户型号:
const userSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
birthday: {
type: Date,
required: true,
},
gender:{
type: String,
required: true
},
country:{
name: {
type: String,
required: true
},
flag: {
type: String,
// default: "/images/flags/US.png"
}
},
avatar: AvatarSchema,
displayName: String,
bio: String,
coverColor: {
type: String,
default: "#343a40"
},
posts: [
{
type: Schema.Types.ObjectId,
ref: "Post"
}
],
comments: [
{
type: Schema.Types.ObjectId,
ref: "Comment"
}
],
postedToday: {
type: Boolean,
default: false
},
todaysPost: {
type: String
}
})
像这样的东西
Post.aggregate([
{$match: {"date": today}},
{$group: {_id: {"country": "$author.country.name"}, avgRating: {$avg: "$rating"}}}
]).then(function(res) {
console.log(res)
})
推荐阅读
- laravel - 在 laravel eloquent 中求和两列
- python - TypeError:必须在散列 python2 工作但 py3 不工作之前对 Unicode 对象进行编码
- android - 如何共享未在应用内浏览器中打开的链接?
- ios - Swift UI - Wheel Picker,更改对齐方式和字体大小
- node.js - 关于 Bob 大叔干净的架构倒置依赖和 Node.js
- asp.net-core - 一起构建 .NET Standard 和 asp.net core
- jquery - 如何在引导弹出窗口中获取数据
- python-3.x - 无法从内部文件夹、OneDrive、Microsoft Graph、Python 下载文件
- php - 数据库上显示的表情符号未正确显示
- bootstrap-4 - Boostrap 4 多选