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):
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?