MongoDb Database query for Geo Near using c#

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Incomplete
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.0.1
    • Component/s: API
    • Environment:
      Windows
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      I am using Mongo CSharpDriver-2.0.0 version and IMongoCollection Async in my application. My data collection looks as shown in the figure attached(2.png).
      Here I want to perform a Search operation on Venue Details, basically we will give a pair of Latitude and Longitude and it should return a list of Records that are with in the distance of 4000 meters from provided Latitude and Longitude.
      To implement above functionality I am using the below code: This code works fine when I pass a single category.

      IMongoCollection<BusinessDeal> BusinessCollection;
      BusinessCollection = _database.GetCollection<BusinessDeal>("BusinessDeals2");

      double? rangeInMeters = null;
      if (rangeInMeters == null)

      { rangeInMeters = 3000; }

      //add index for venue.location and category
      var keyBuilder = Builders<BusinessDeal>.IndexKeys;
      keyBuilder.Geo2DSphere(x => x.Venuedetails.Select(y => y.Location)).Ascending(x => x.Categories.Select(y => y.Id));
      keyBuilder.Geo2DSphere(x => x.Venuedetails.Select(y => y.Location)).Ascending(x => x.Accessgroups.Select(y => y.Id));

      BsonDocument geoPoint = new BsonDocument
      {

      {"type","Point"}

      ,
      {"coordinates",new BsonArray(new double[]{-84.289156, 34.289156})}
      };

      BsonDocument geoNearOptions = new BsonDocument
      {

      {"spherical", true}

      ,

      {"limit", 300}

      ,should we do a filter first?

      {"maxDistance", rangeInMeters}

      ,

      {"near",geoPoint}

      ,

      {"distanceField","dist"}

      };

      var stage = new BsonDocumentPipelineStageDefinition<BusinessDeal, BusinessDeal>(new BsonDocument {

      { "$geoNear", geoNearOptions }

      });

      var colAggregate = BusinessCollection.Aggregate().AppendStage(stage);
      var result = BusinessCollection.Aggregate().ToListAsync();
      _businessDealsList = result.Result;

      The above code is returning all the records that are in the Collection. But there are only 2 records that are 4000 meters of distance. My records are as follows:
      Latitude1 Longitude1 Latitude2 Longitude2 Distance from Origin in Meters
      Record1 -84.289156 34.289156 -84.288357 34.068686 2414.016
      Record2 -84.289156 34.289156 -84.299477 34.069886 2735.8848
      Record3 -84.289156 34.289156 -84.299477 34.690986 4667.0976

      When I run or Debug the code, it returns all the three records. But it should return only 2 records. Please review the code that I have written and help where I am going wrong. Thanks in advance for your help and support.

        1. 2.png
          2.png
          65 kB

            Assignee:
            Unassigned
            Reporter:
            Arvind Chary
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: