Cannot use Mongo's Filter with DateTime and BsonString?

孤者浪人 提交于 2021-01-29 15:23:50

问题


I'm using C3 with mongo and I'm trying to query documents by dateCreated property.

For example , if I extract the first record by :

var models = await c.FindAsync(a=>true);
var list = await models.ToListAsync();
list.First().Dump();

Then I see the result :

Please notice that the value is BsonString , and that I have no entity behind it.

The problem is that now , I want to query the collection based on datetime :

So I did this :

var filter = Builders<BsonDocument>
.Filter
.And
(
    Builders<BsonDocument>.Filter.Gte(x => x["dateCreated"].AsBsonDateTime , DateTime.Today.AddDays(-10000)) // old enough
);
 
var models = await c.FindAsync(filter);
var list = await models.ToListAsync();
list.First().Dump();

But I get : InvalidOperationException: Sequence contains no elements

Question:

How can I query the collection based on datetime knowing that the collection date is stored as BsonString ?

来源:https://stackoverflow.com/questions/65503195/cannot-use-mongos-filter-with-datetime-and-bsonstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!