cはMongoDBの内の1つの配列にコレクションのすべてのネストされた配列に参加しますは、私がこのような構造を持つ文書を持っている#
["111111111111111111111111","222222222222222222222222","333333333333333333333333","444444444444444444444444"]
さて、私はMongoDB.Dを使用して、次のようにやっている:私はのような配列がしたい
{
"_id" : ObjectId("5a26f1764cacd91bf4624a76"),
"CompetitorIds" : [
"111111111111111111111111",
"222222222222222222222222"
],
"BusinessId" : "5a1bfcac2b8fb6096885cbb8"
}
{
"_id" : ObjectId("7a26f1764cacd91bf4112a88"),
"CompetitorIds" : [
"333333333333333333333333",
"444444444444444444444444"
],
"BusinessId" : "5a1bfcac2b8fb6096885cbb8"
}
川からC#:
public class Product
{
public ObjectId Id { get; set; }
public string[] CompetitorIds { get; set; }
public string BusinessId { get; set; }
}
public List<string> GetAllCompetitors(string businessId)
{
var builder = Builders<Product>.Filter;
var filter = builder.Eq(p => p.BusinessId, businessId) & builder.Where(p => p.CompetitorIds.Length > 0);
var collection = _collection.Find(filter);
var productIdsAlreadyBound = collection.ToList().SelectMany(x => x.CompetitorIds).ToList();
}
しかし、私はそれを行うより良い方法があるかどうかを知りたいと思います。助言がありますか?
'VAR productIdsAlreadyBound = _collection.Distinct( "competitorIds"、フィルタ)試してみてください;'や 'のFieldDefinitionフィールド= "CompetitorIdsを"。 var productIdsAlreadyBound = _collection.Distinct(field、filter); ' –
Veeram
@Veeram動作しませんでした。 –
@Veeramが提供している例では、 "CompetitorIds"の末尾のスペースを必ず削除してください。また、小文字のバージョンを使用する必要があるかもしれません。なぜなら、C#の表現に合わせるために変更する前に、サンプルデータが含まれているからです。 – dnickless