Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb C# compare date without time

How to compare date without time in mongodb C# driver?

I try to use this code, bot it don't work.

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

Do you have any ideas?

like image 886
user2614682 Avatar asked Dec 21 '22 02:12

user2614682


1 Answers

Query for range of dates, which include your date:

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

Same with LINQ (MongoDB.Driver.Linq namespace):

var result = from l in _logCollection.AsQueryable()
             where l.Date >= beginDate && l.Date < endDate
             select l;
like image 188
Sergey Berezovskiy Avatar answered Jan 10 '23 23:01

Sergey Berezovskiy