Reputation: 507
Suppose I have database named GSCADB and collection named GSCALogs, and each documnet looks like:
{
"_id": "5d6f514c19038b8b38aec8d7",
"SHA-256": "839c95cb99e8243d762ccb6f33ed8e1550b6848f739556e71dc8bcf684a159c5",
"File Name": "Settings.settings",
"File Name (GUID)": "69AA3BA5-D51E-465E-8447-ECAA1939739A",
"New File Name": "Settings.settings",
"File Size (Bytes)": "1379",
"Result": "Ok",
"File type": "settings",
"True File type": "txt;htm;html",
"Start Job Date": "2019-09-04T05:53:43.397Z"
}
and I want to know how many documents have Rsult as OK.
For now I have:
IMongoDatabase db = dbClient.GetDatabase("GSCADB");
var collection = db.GetCollection<BsonDocument>("GSCALogs");
How can I proceed? should I create some object? how the class of this object should look like?
I mean like here, there is C# object named Employee
, and also in this link there is object named User
, should I create an object also?
Upvotes: 0
Views: 311
Reputation: 1329
Give this a try
// Define the filter
var filter = new BsonDocument("Result", "Ok");
// Run the count method on the collection filtering for the required docs
var documentCount = collection.CountDocuments(filter);
This only gives you the count. If you need the docs as well you can
var countedDocuments = collection.Find(filter).ToList();
Additional info on the available Count
methods (depending on your driver version) can be found at https://mongodb.github.io/mongo-csharp-driver/2.9/apidocs/html/T_MongoDB_Driver_MongoCollectionBase_1.htm
Upvotes: 1