Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
How to get average an attribute of an array in mongodb (mongoose)

please help me, I'm trying to get near sellers by users location input and then sort by average rating. Is it possible? This is the snippet of the model. Model has an array of reviews.

    const sellerSchema = new mongoose.Schema({
        _id: Mongoose....ObjectId
            //... 
        reviews: [
        {
          by: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User",
          },
          title: {
            type: String,
          },
          message: {
            type: String,
          },
          rating: Number,
          imagesUri: [{ String }],
          timestamp: {
            type: Date,
            default: Date.now,
          },
        },
      ],
    });

Then my aggregate I have:

const seller = await Seller.aggregate(
    [
      {
        $geoNear: {
          near: {
            type: "Point",
            coordinates: [longitude, latitude],
          },
          distanceField: "distance",
          spherical: true,
          maxDistance: radius,
        },
      },
      rating_minimum ? { $match: { rating: { $gt: rating_minimum } } } 
       : {},
      {$limit: limit},
    ]);

I was thinking of $group and create avgReview, and then sort them by reviews something like:

{$group:{averageReviews: { $avg: "$reviews.rating"}},
{$sort: { averageReviews: 1 } },
{$limit: limit}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since the reviews are in array per document, instead of $group which collect documents, you can use $reduce to calculate the average rating:

 {
    $addFields: {
      ratingSum: {
        $reduce: {
          initialValue: 0,
          input: "$reviews",
          in: {$sum: ["$$value", "$$this.rating"]}
        }
      }
    }
  },
  {
    $addFields: {
      "averageReviews": {"$divide": ["$ratingSum", {$size: "$reviews"}]
      }
    }
  },
  {$sort: { averageReviews: 1 } },
  {$limit: limit}

As you can see on this playground example.

I'm not sure about what your are trying to limit for 3, but if it is the final results, 3 sellers, than you are using it correctly.

BTW, your seller schema is missing the location of the seller, needed for the $geoNear.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda