[英]How to find items in a collection that nested array field contains specific string using mongodb .net driver C#?
[英]How to filter by a TimeSpan field stored as string using mongodb c# driver?
我有一個mongodb集合,其中的字段以字符串格式存儲時間。 在我的C#代碼中,模型具有TimeStamp類型的屬性:
public TimeSpan StartTime { get; set; }
我需要查詢以獲取StartTime大於UtcNow的所有文檔。 就像是:
var now = new TimeSpan(DateTime.UtcNow.Hour, DateTime.UtcNow.Minute, DateTime.UtcNow.Second);
...
...
var docs = db.GetCollection<DocDataModel>("docs")
.AsQueryable()
.Where(e => e.StartTime > now)
但是我收到一個InvalidOperationException消息:不支持Where(({{document} {StartTime}> 12:04:52)))。
有什么辦法可以正確編寫此查詢嗎?
使用TimeSpan.Ticks
將StartTime
和now
變量轉換為long
var now = new TimeSpan(DateTime.UtcNow.Hour, DateTime.UtcNow.Minute, DateTime.UtcNow.Second).Ticks;
...
...
var docs = db.GetCollection<DocDataModel>("docs")
.AsQueryable()
.Where(e => e.StartTime.Ticks > now)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.