Home > OS >  How to get average an attribute of an array in mongodb (mongoose)
How to get average an attribute of an array in mongodb (mongoose)

Time:05-05

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}

CodePudding user response:

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.

  • Related