Как добавить запрос в geoNear в MongoDB, используя драйвер C# 2.8.1? - PullRequest
0 голосов
/ 09 января 2020

У меня есть метод, подобный следующему:

public IEnumerable<Child> GetCandidates(Query query)
{
    //This collection is created on the fly, so it is not strongly typed
    var collection = Db.GetCollection<BsonDocument>($"ScoreResult_{query.SearchId}");

    var filterBuilder = Builders<BsonDocument>.Filter;
    var filter = filterBuilder.Empty;

    if (query.CandidateStatus != null)
        filter &= filterBuilder.Eq("Status", query.CandidateStatus);
    if (query.JobType != null)
        filter &= filterBuilder.Eq("PreferredJobType", query.JobType);

    if(!query.Distance.HasValue)
        var result = collection.Aggregate()
                                .Match(filter)
                                .Group(Grouping)
                                .Project(Projection)
                                .Sort(Sorting)
                                .Skip(query.PageNumber * query.PageSize)
                                .Limit(query.PageSize)
                                .Lookup("Candidates", "_id", "_id", "DataCandidate")
                                .ToList();

   if(query.Distance.HasValue) // Here I need to extend the query for distance
   {
       // Here I need to build a query like following
       // with the Group, Project, Sort, Skip, Limit, Lookup used in above.

       // I think $geoNear will be the first stage of the above aggregation.

       collection.aggregate([
       {
           $geoNear: {
               near: { type: "Point", coordinates: [ -73.9855426, 40.7579747 ] },
               spherical: true,
               query: /*Previously created "filter" here*/,
               maxDistance: query.Distance.Value,
               distanceMultiplier: .001,
               distanceField: "Coordinates"
           }
        }
       ])
   }

   return result;

}
...