1

I have a little project in C# which consists of an API that receives data about (made up) infection cases. Each case object have the geographic coordinates of such case.

The program is supposed to query the nearest city to that case and return it as a response. I try to do this with the following method:

private void ObterCidadeMaisProxima(Infectado infectado){
    var collection = GetCidadeCollection();

    string coordinates = $"[ {infectado.Localizacao.Latitude}, {infectado.Localizacao.Longitude} ]";

    string query = "{ location: { $near : { $geometry: { type: \"Point\",";

    query += $"coordinates: {coordinates}";

    query += "}, $maxDistance: 1000000 } } }";

    var e = BsonDocument.Parse(query);
    var doc = new QueryDocument(e);

    var result = collection.Find<Cidade>(doc).ToList<Cidade>();

    //So far I just want to test the function, that's why I don't return the result         
    foreach(var r in result) Console.WriteLine(r);
}

Both my classes (Infectado and Cidade) have the following attribute:

public GeoJson2DGeographicCoordinates Localizacao { get; set; }

And in MongoDB Atlas I have created the following indexes for both collections (infectado and cidade):

Geospatial indexes

So, I have two collections using geospatial indexes, and I'm trying to execute a $near query, but MongoDB can't find the indexes.

Here's the output:

fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HM7HA1MKQ9RL", Request id "0HM7HA1MKQ9RL:00000001": An unhandled exception was thrown by the application. MongoDB.Driver.MongoCommandException: Command find failed: error processing query: ns=covidDB.cidadeTree: GEONEAR field=location maxdist=1e+06 isNearSphere=0 Sort: {} Proj: {} planner returned error :: caused by :: unable to find index for $geoNear query.

What's wrong?

  • [GeoJSON Coordinate](https://docs.mongodb.com/manual/geospatial-queries/#geospatial-geojson) is `[longitude, latitude]`, you did `[latitude, longitude]` – Wernfried Domscheit Mar 27 '21 at 19:57
  • @WernfriedDomscheit fixed it, thanks; still not working, though :( – potato_free Mar 27 '21 at 20:18
  • Verify indexes are created on the same collection you are querying. Then test everything using mongo shell. – D. SM Mar 28 '21 at 11:34
  • @D.SM I have only two collections, and both have indexes on the coordinates (I even create a 2d and a 2dsphere on each one, just to be sure), and the $near queries works fine on MongoDB Compass. I haven't tested on shell, though; it refuses to connect for some reason. – potato_free Mar 28 '21 at 15:53
  • That is not verification. From the same place you are querying, issue the query to read indexes. – D. SM Mar 28 '21 at 17:01
  • Did you tried this: https://stackoverflow.com/questions/23188875/mongodb-unable-to-find-index-for-geonear-query – KushalSeth Mar 28 '21 at 21:04
  • @D.SM Just did it, and the result is the same as the shell's: it shows the indexes as I have created them. – potato_free Mar 28 '21 at 22:48
  • @KushalSeth Yes. – potato_free Mar 28 '21 at 22:48

0 Answers0