如何在mongodb C#驱动程序中不加时间地比较日期?
我尝试使用此代码,但它不起作用。
MongoCursor<Log> query = _logCollection.FindAs<Log>(
Query.And(Query.EQ("Date.getDate()", date.Day),
Query.EQ("Date.getMonth()", date.Month),
Query.EQ("Date.getYear()", date.Year)));
你有什么想法吗?
答案 0 :(得分:10)
查询日期范围,包括您的日期:
var beginDate = date.Date; // e.g. 7/24/2013 00:00:00
var endDate = beginDate.AddDays(1); // e.g. 7/25/2013 00:00:00
var query = Query.And(Query<Log>.GTE(l => l.Date, beginDate), // including
Query<Log>.LT(l => l.Date, endDate)); // not including
var result = _logCollection.FindAs<Log>(query);
与LINQ( MongoDB.Driver.Linq 命名空间)相同:
var result = from l in _logCollection.AsQueryable()
where l.Date >= beginDate && l.Date < endDate
select l;