C# Mongodb驱动程序中的BsonRegularExpression不';t返回所需的值
假设我有一个名为GSCADB的数据库和一个名为GSCALogs的集合,每个文档如下所示:C# Mongodb驱动程序中的BsonRegularExpression不';t返回所需的值,c#,json,regex,mongodb,C#,Json,Regex,Mongodb,假设我有一个名为GSCADB的数据库和一个名为GSCALogs的集合,每个文档如下所示: { "_id": "5d6f514c19038b8b38aec8d7", "SHA-256": "839c95cb99e8243d762ccb6f33ed8e1550b6848f739556e71dc8bcf684a159c5", "File Name": "Settings.settings", "Start Job Date": "2019-09-04T05:53:43.
{
"_id": "5d6f514c19038b8b38aec8d7",
"SHA-256": "839c95cb99e8243d762ccb6f33ed8e1550b6848f739556e71dc8bcf684a159c5",
"File Name": "Settings.settings",
"Start Job Date": "2019-09-04T05:53:43.397Z"
}
我想知道有多少文档具有以2019-09开始的Start Job Date
目前我有:
IMongoDatabase db = dbClient.GetDatabase("GSCADB");
var collection = db.GetCollection<BsonDocument>("GSCALogs");
var filter = Builders<BsonDocument>.Filter.Regex("Start Job Date", new BsonRegularExpression("^(2019-09)"));
var countedDocuments = collection.Find(filter).ToList();
MessageBox.Show(countedDocuments.Count.ToString());
IMongoDatabase db=dbClient.GetDatabase(“GSCADB”);
var collection=db.GetCollection(“GSCALogs”);
var filter=Builders.filter.Regex(“开始作业日期”,新的BsonRegularExpression(“^(2019-09)”);
var countedDocuments=collection.Find(filter.ToList();
Show(countedDocuments.Count.ToString());
但是它总是打印
0
,我缺少什么?您可以使用此mongodb查询获取输出
db.getCollection('GSCALogs').find({Start Job Date:{$regex:new RegExp('^.'+'2019-09'+'.','i')}}).count()
- 它将返回“开始作业日期”为“2019-09”的实际计数
- 它将返回“开始作业日期”为“2019-09”的实际计数
...
var filter = Builders<BsonDocument>.Filter.Regex("Start Job Date", new BsonRegularExpression("^(2019-09)","i"));
var countedDocuments = collection.Find(filter).ToList();
MessageBox.Show(countedDocuments.Count.ToString());
。。。
var filter=Builders.filter.Regex(“开始作业日期”,新的BsonRegularExpression(“^(2019-09)”,“i”);
var countedDocuments=collection.Find(filter.ToList();
Show(countedDocuments.Count.ToString());
在将日期作为字符串写入数据库后,我认为问题出在哪里,它被保存为datetime而不是string,它使用此代码:
...
var filter = Builders<BsonDocument>.Filter.Regex("Start Job Date", new BsonRegularExpression("^(2019-09)","i"));
var countedDocuments = collection.Find(filter).ToList();
MessageBox.Show(countedDocuments.Count.ToString());
。。。
var filter=Builders.filter.Regex(“开始作业日期”,新的BsonRegularExpression(“^(2019-09)”,“i”);
var countedDocuments=collection.Find(filter.ToList();
Show(countedDocuments.Count.ToString());
您是否先尝试使用mongo shell或某个客户端程序对mongo进行查找?另请注意:您可以调用.Count(filter),这样mongo将为您计算元素,而不是检索提升内存中结构的所有文档和counting@PabloRecalde文档存在,我可以在MongoDB Compass
@PabloRecalde中看到它,我将接受.Count(filter)
建议..是,但是你有没有像在c#代码中那样尝试用正则表达式过滤器来查询它?你有没有尝试过使用mongo shell或一些客户端程序来首先对mongo进行测试?另请注意:您可以调用.Count(filter),这样mongo将为您计算元素,而不是检索提升内存中结构的所有文档和counting@PabloRecalde文档存在,我可以在MongoDB Compass
@PabloRecalde中看到它,我将接受.Count(filter)
建议..是,但是你有没有像在c#代码中那样用正则表达式过滤器来查询它?