在c#中使用$ geoWithin运算符

时间:2013-06-19 18:49:16

标签: c# mongodb mongodb-.net-driver

目前在一个项目中,我使用查询来查找x英里内的集合中的所有内容。阅读mongodb文档,它声明操作符中的$已被弃用,您应该使用$ geoWithin查询运算符。

我正在使用Mongodb查询构建器(见下文)

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

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

1 个答案:

答案 0 :(得分:1)

我对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

由于库是开源的,您可以对其进行分叉,编辑和重新编译,并使用您自己的版本。此外,您可以提出拉取请求,建议您将更改包含在主代码中。

我认为没有“更容易”的方法,我希望这会有所帮助。