Seeking assistance with a query to find sellers near users based on location input and sorting them by average rating. Is this achievable? Snippet of the model including 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,
},
},
],
});
Aggregate function used as follows:
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},
]);
Consideration for using $group to calculate avgReview and then sort by reviews:
{$group:{averageReviews: { $avg: "$reviews.rating"}},
{$sort: { averageReviews: 1 } },
{$limit: limit}