如何在monogdb c#

时间:2018-04-16 07:58:23

标签: c# mongodb linq iqueryable

我从mongoDB C#驱动程序1.11升级到2.5.0,并且在使用LINQ时我无法再执行$ geoNear。

据我所知,LINQ创建了一个聚合查询,$ geoNear不能在聚合查询中,除非它是第一阶段。 如何将$ geoNear阶段与任何IQueryable结合起来?任何建议都将不胜感激。

//valid Bson example that work in aggregate query
new BsonDocument
        {
            {"$geoNear", new BsonDocument
                {
                    {"near", new BsonDocument
                        {
                            {"type", "Point"},
                            {"coordinates", new BsonArray(new[] {45.73, 72.81})}
                        }
                    },
                    { "maxDistance", 100},
                    {"distanceField", "Calculated"},
                    {"spherical", true}
                }
            }
        };

 class Entity
    {
        public IList<Coordinates> Locations { get; set; }
        public int NumField { get; set; }
        public double Calculated { get; set; }
    }

Fyi,我之所以使用IQueryable是因为我不希望与我的数据库耦合来对mongoDB执行查询。

之前的代码是:

private IQueryable<T> GetBsonRangeQuery(IQueryable<T> result) {
  BsonDocument coordinatesBson = Query.Near(
    fieldName,
    GeoJson.Point(GeoJson.Geographic(45.73, 72.81)),
    100,
  true).ToBsonDocument();
  BsonDocument existingFilterBson = ((MongoQueryable<T>) result).GetMongoQuery()
    .ToBsonDocument();
  BsonDocument combined = existingFilterBson.AddRange(coordinatesBson);
  return GetInitializedMongoCollection().AsQueryable().Where(
    q => new QueryDocument(combined).Inject());
} 

0 个答案:

没有答案
相关问题