我希望使用 C# 驅動程序(V 2.7.0)來聚合一個集合,然后計算結果。假設我有一個 IMongoCollection col,我會這樣聚合:IAsyncCursor<BsonDocument> cursor = col.Aggregate() .Match(someFilterDefinition) .Project(someProjectionDefinition) .Unwind("someFieldName") .ToCursor();while (cursor.MoveNext()){ foreach (BsonDocument doc in cursor.Current) { doStuff(doc); }}我希望獲得聚合返回的文檔數量。在外殼中你會做這樣的事情db.getCollection("someCollectionName").aggregate( [ { "$match" : { // some match filter } }, { "$project" : { "someField" : 1 } }, { "$unwind" : "$someField" }, { "$count" : "count" } ], { "allowDiskUse" : false });你會得到一個帶有單個“count”字段的文檔(或沒有文檔),它的前一個階段的元素數量的 int64 值。有一個IAggregateFluent.Count()函數。但是它返回一個 IAggregateFluent 我想要一個 AggregateCountResult 或者只是一個簡單的無符號長。如何使用 Aggregate.Count() 函數?
MongoDB C# 聚合計數
慕的地8271018
2022-06-19 10:24:16