Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda