在c#中使用$geoWithin操作符

本文关键字:geoWithin 操作符 | 更新日期: 2023-09-27 18:19:01

目前在一个项目中,我正在使用查询来查找x英里数内的集合中的所有内容。阅读mongodb文档,它指出$within操作符被弃用,你应该使用$geoWithin查询操作符。

我正在使用Mongodb查询生成器(见下文)

Query<Stuff>.WithinCircle(x => x.LongLat, longitude, latitude, radians, true)

,我注意到它创建的查询使用$within操作符,而不是$geoWithin操作符。我没有看到任何方法来更新它以使用正确的操作符,因为我们已经升级到Mongodb 2.4.x

在c#中使用$geoWithin操作符

我不太了解MongoDB,但似乎MongoDB -csharp代码尚未更新为使用$geoWithin,正如您在这里看到的:

mongo-csharp-driver (QueryBuilder)

public static IMongoQuery WithinCircle(string name, double centerX, double centerY, double radius, bool spherical)
{
    if (name == null)
    {
        throw new ArgumentNullException("name");
    }
    var shape = spherical ? "$centerSphere" : "$center";
    var condition = new BsonDocument("$within", new BsonDocument(shape, new BsonArray { new BsonArray { centerX, centerY }, radius }));
    return new QueryDocument(name, condition);
}

看,它使用$within

由于库是开源的,您可以分叉、编辑和重新编译它,并使用您自己的版本。另外,你可以提出一个pull request来提议你的修改被包含在主代码中。

我认为没有"更简单"的方法,我希望这对你有帮助。