【问题标题】:How to "SELECT WHERE" in mongodb with MongoDB driver如何使用 MongoDB 驱动程序在 mongodb 中“选择 WHERE”
【发布时间】:2019-09-04 06:22:59
【问题描述】:

假设我有名为 GSCDB 的数据库和名为 GSCALogs 的集合,每个文档网看起来像:

{
    "_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"
}

我想知道有多少文档的结果为 OK。

现在我有:

IMongoDatabase db = dbClient.GetDatabase("GSCADB");
var collection = db.GetCollection<BsonDocument>("GSCALogs");

我该如何继续?我应该创建一些对象吗?这个对象的类应该是什么样子的?

我的意思是像here,有一个名为Employee 的C# 对象,还有in this link 有一个名为User 的对象,我也应该创建一个对象吗?

【问题讨论】:

标签: c# json mongodb


【解决方案1】:

试试这个

// 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);

这只会给你计数。如果你也需要这些文档,你可以

var countedDocuments = collection.Find(filter).ToList();

有关可用Count 方法(取决于您的驱动程序版本)的更多信息,请访问https://mongodb.github.io/mongo-csharp-driver/2.9/apidocs/html/T_MongoDB_Driver_MongoCollectionBase_1.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多