1
dbsampleから:MongoDBのGET平均各arrayelement
var doc1= new BsonDocument
{
{ "line", "a" },
{ "size", new BsonArray { 2 } }
};
var doc2= new BsonDocument
{
{ "line", "xx" },
{ "size", new BsonArray { 5, 3, 8} }
};
var doc3= new BsonDocument
{
{ "line", "xx" },
{ "size", new BsonArray { 1, 10, 8} }
};
私はグループに線でBsonDocumentをしたい、各ラインの数とすべてのBsonArrayElementの平均を取得します。グループ化とカウントは機能しますが、すべてのBsonArrayElementの平均を取得するにはどうすればよいですか? BsonArrayの長さは、異なる行の間で異なります。
var filterBuilder = Builders<BsonDocument>.Filter;
var aggregate = collection.Aggregate().Group(new BsonDocument { { "_id", "$line" }, { "count", new BsonDocument("$sum", 1) } });
foreach (var document in aggregate.ToEnumerable())
{
Console.WriteLine(document);
}
私が欲しいの出力:
{ "_id" : "xx", "count" : 2, "avg" : [3, 6.5, 8] }
{ "_id" : "a", "count" : 1, "avg" : [2]}
ありがとうございます。コードはシェルで動作しますが、C#でエラーが発生します。 "'BsonDocument'には 'doc'の定義が含まれておらず、拡張メソッド 'doc'は 'BsonDocument'型の最初の引数を受け入れることができませんでした。 ) " – seeberg
私が答えで述べたように、私はこれをテストしていません。私がチャンスを得るとき、私はリファクタリングを必要とするかもしれないこのステップ '.Unwind(x => x.doc)'で特にパイプラインをデバッグする必要があるかもしれません。 – chridam
私はそれを働かせることができません:( – seeberg