.netmongodbmongodb-querymongodb-.net-drivermongodb-csharp-2.0

MongoDB FilterDefinition & IQueryable in C#


I have the following spatial FilterDefinition:

var filter = Builders<MyDocument>
                .Filter
                .Near(x => x.Point, point, 1000);

Is there any way to include this into an IQueryable expression?

For example, I might have the following LINQ statement. How can I include the above condition? From what I can see, there is no LINQ support for spatial querying.

return Database
    .GetCollection<Places>("Places")
    .AsQueryable()
    .Where(x => x.StartDate.Date <= date)
    .Where(x => x.EndDate.Date >= date)
    .Where(x => keys.Contains(selectedKeys))
    .ToList();

I am using the new 2.2.2 libraries.


Solution

  • There is a feature request in the .NET drivers jira project: https://jira.mongodb.org/browse/CSHARP-1445. So, the answer is currently no, but hopefully soon.

    However, there is a "Where" method on the FilterDefinitionBuilder (https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/FilterDefinitionBuilder.cs#L1286) that will allow you to include a LINQ predicate into normal find/aggregation queries.