sql >> Base de Datos >  >> NoSQL >> MongoDB

Mongoose/Mongodb Aggregate:agrupar y promediar varios campos

Esto funcionó para mí:

  Post.aggregate([
    { $group: { _id: "$date", avgRating: { $avg: '$rating' }}}
  ]).
  then(function (res) {
    console.log(res); 
  })

Salida:

[
  { _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 }
]

PERO sería genial si pudiera filtrar esto de alguna manera en función de otros factores. Por ejemplo, cada publicación tiene un autor (referencia al modelo de usuario). ¿Cómo haría para filtrar según el país, el nombre o el sexo del autor?

Modelo de usuario:

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
}
}) 

Algo como esto

    Post.aggregate([
    {$match: {"date": today}},
    {$group: {_id: {"country": "$author.country.name"}, avgRating: {$avg: "$rating"}}}
]).then(function(res) {
    console.log(res)
})