我在mongoDB中有以下查询
pdb.getCollection('articulo').aggregate(
{
$match: { $text: { $search: "monitor" } } ,
},
{
$project: {
_id: 1,
titulo :1,
ranking : {
$avg: "$valoracion.valor"
}
}
},
{
$sort : {
ranking : -1 , fechaAlta : -1
}
});
我试图将其转换为C#查询(MongoDB.Driver),但我在投影中的计算字段(平均值)存在问题。
我的问题是: 如何在投影中获得计算字段("排名")?
这就是我所拥有的:
public async Task<List<T>> All<T>(string searchBy, int page, int size) where T : class, new()
{
try
{
var projectionSelect = Builders<T>.Projection.Include("_id")
.Include("titulo")
//.Include(new BsonDocument { { "rating", new BsonDocument { { "$avg", "$valoracion.valor" } } } }); that didn´t wokt for me
return await _dataBase.GetCollection<T>(GetName<T>()).Aggregate().Project<T>(projectionSelect)
.Sort(Builders<T>.Sort.Descending("ranking")).Skip(page).Limit(size).ToListAsync();
}
catch (Exception ex)
{
Log.Instance.LogErrors(ex);
throw;
}
}
我提前感谢您的帮助