我正在使用 $geoNear 聚合阶段的 .NET 应用程序中运行 MongoDB 查询,并返回指定距离内的对象列表。它以以下格式返回它们(我使用 MongoDB 示例页面中的结果进行简化):
{
"_id" : 8,
"name" : "Sara D. Roosevelt Park",
"location" : {
"type" : "Point",
"coordinates" : [
-73.9928,
40.7193
]
},
"category" : "Parks",
"distance" : 974.175764916902
}
这本质上是我的输出类型,添加了“距离”字段。我想要做的是将它们分成不同的类,所以它看起来像这样:
public class EventLocationSearchResult
{
public Event Event { get; set; }
public double Distance { get; set; }
}
到目前为止,我的代码如下所示:
var result = await events
.Aggregate()
.AppendStage(GeoHelper.GetGeoNearStage<Event>(lon, lat, distance))
.Lookup<User, Event>(_usersCollectionName, "memberIds", "id", "Members")
.ToListAsync();
GeoHelper.GetGeoNearStage(..)看起来像这样:
public static BsonDocumentPipelineStageDefinition<TNewResult, TNewResult> GetGeoNearStage<TNewResult>(double lon, double lat, double distance, string dbField)
{
var geoNearOptions = new BsonDocument
{
{ "near", new GeoJsonPoint<GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(lon, lat)).ToBsonDocument()},
{ "maxDistance", distance },
{ "distanceField", "Distance" },
{ "spherical", true }
};
var stage = new BsonDocumentPipelineStageDefinition<TNewResult, TNewResult>(new BsonDocument { { "$geoNear", geoNearOptions } });
return stage;
}
我能以某种方式做到吗?谢谢你的帮助!