【发布时间】:2018-03-04 13:09:44
【问题描述】:
我正在尝试获取最近的记录,如下所示,但排序操作抱怨表达式不允许。如何在 MongoDB 中实现这一点?
错误:
NotSupportedException:$sort 中只允许使用字段。 MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateOrderBy(OrderByExpression 节点)
代码:
public virtual List<Post> PagingNearest(Expression<Func<Post, bool>> predicate, int offset, int limit, GeoCoordinatePortable.GeoCoordinate coord)
{
return _context.Posts.AsQueryable()
.Where(predicate)
.OrderBy(x => x.Location.GeoCoordinate.GetDistanceTo(coord))
.Skip(offset)
.Take(limit)
.ToList();
}
【问题讨论】:
-
您是否尝试查询包含 GeoJSON 坐标的多个文档,并根据与特定点的距离对其进行排序?
标签: c# mongodb asp.net-core-2.0 mongodb-.net-driver