当字段以字符串数组的形式给出时,如何在新的MongoDB C驱动程序中对字段进行投影?.
我可以在一个领域找到方法
collection.find(filter).Project(Builders<Category>.Projection.Include(fieldName)
如何扩展它以获取字段数组?.

最佳答案

也有扩展方法Include

var projection = Builders<Category>.Projection.Include(fieldList.First());
foreach (var field in fieldList.Skip(1))
{
    projection = projection.Include(field);
}
var result = await collection.Find(filter).Project(projection).ToListAsync();

08-06 16:34