I want to calculate distance inside lookup operator

Given user location : [latitude, longitude]

RatingSchema : { company_id : xxxxx, rating : 3.44}.
CompanySchema : {comapany_id : xxxxx, company_name : "hdhh", location : {type : "Point" , coordinates : [77.6666, 12.4333]}}
    {$group : {Id : "$company_id", Num_rating: {$sum : 1}}},
     {$match : {Num_rating : {$gte : 3}}},
         $lookup :{
             from : "companies",

If we can use GeoNear operator, then It is very easy but we can’t use Geonear here.
Now , We have to send the information of those companies with distance which have at least 3 ratings.
Please tell me how can I do it ?

Hi @Praveen_Gupta

In general I don’t see why the rating is not in the companies collection. The rating is per company, so in MongoDB there is no need to split those as they are related and required in a single query.

Now in anyway you need to first query companies and get the ones that are near a user with a geoNear stage.

Once you find those you can lookup ratings and and use a pipeline syntax where you will do the same group for the current company. Once you have the groups you can filter based on the rating numbers with $match.

If you need more specific guidance let me know.


1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.