フィールドが文字列配列の形式で指定されている場合、新しい MongoDB C# ドライバーのフィールドにどのように投影しますか?. 次のようにして、単一のフィールドに投影する方法を見つけることができました
collection.find(filter).Project(Builders<Category>.Projection.Include(fieldName)
これを拡張してフィールドの配列を取得するにはどうすればよいですか?.
フィールドが文字列配列の形式で指定されている場合、新しい 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();
mofenko よりも優れた方法として、最初の列を含める必要はありません。
ProjectionDefinition<BsonDocument> project = null;
foreach (string columnName in columnsToInclude)
{
if (project == null)
{
project = Builders<BsonDocument>.Projection.Include(columnName);
}
else
{
project = project.Include(columnName);
}
}
これは緩やかに型付けされたデータ用です。クラスを使用している場合は、クラスに置き換えBsonDocument
ます