RobThree/MongoRepository

Unable to write where condition on dates

bhanu7755 opened this issue · 6 comments

Hi, I'm unable to write where condition on dates. Whenever I compare the dates it was just hanging. Here is the example

var data = postrepo.Where(x => x.CreatedDate== DateTime.Now);

First of all I doubt you want that exact expression; x.CreatedDate seems unlikely to ever equal DateTime.Now (that would mean it would have to match up to 1/1000th of a millisecond or something around that order). You probably want to compare dates (something like x.CreatedDate.Date == DateTime.Now.Date) where you use the Date property to compare dates. I'm not sure the driver can create a good 'mongo query' for this so you might want to test it and if it doesn't work do something like x.CreatedDate > start && x.CreatedDate < end where start is DateTime.Now.Date and end is start.AddDays(1) or something along those lines.

I guess the "hang" is not actually a hang but Mongo taking a long time finding the results you want (or expect). Did you profile your query? My best guess would be you need an index on the CreatedDate field.

Yes.. I tried that as well

var data = postrepo.Where(x => x.CreatedDate.Date== DateTime.Now.Date);

But the result is just same. And I have just three documents nothing more than that. Index should not be a problem.

Yes.. I tried that as well

Tried what as well? You'll have to be a bit more specific (also see your previous issue). I can only help you if you provide enough information (before posting, read your issue as if you know nothing about the problem or what you're doing or explaining it to a stranger) and, if possible, also provide a test-case to reproduce or at the very least steps to reproduce the problem.

I created a testcase which works fine:

using MongoRepository;
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var postrepo = new MongoRepository<Post>();

        postrepo.Add(new Post() { CreateDate = DateTime.Now, Title = "A" });
        postrepo.Add(new Post() { CreateDate = DateTime.Now.AddDays(-1), Title = "B" });
        postrepo.Add(new Post() { CreateDate = DateTime.Now.AddHours(-2), Title = "C" });

        var start = DateTime.Now.Date;
        var end = start.AddDays(1);

        var todaysposts = postrepo.Where(p => p.CreateDate > start 
            && p.CreateDate < end);
    }
}

class Post : Entity
{
    public string Title { get; set; }
    public DateTime CreateDate { get; set; }
}

It turns out (as I guessed) that using the .Date property doesn't work (driver throws a NotSupportedException, as expected/guessed): An unhandled exception of type 'System.NotSupportedException' occurred in MongoDB.Driver.dll So I implemented the other solution / workaround I provided in that answer and that works fine.

Hi Rob, I'm sorry if i'm not clear. I tried the same approach you gave in the above example but my debug point is just disappeared at the below statement

var todaysposts = postrepo.Where(p => p.CreateDate.Date ==start.Date
&& p.CreateDate.Date == end.Date);

but my debug point is just disappeared at the below statement

I'm sorry but I have no idea what that means. Do you get an exception?

I tried the same approach you gave in the above example [...]

var todaysposts = postrepo.Where(p => p.CreateDate.Date ==start.Date 
    && p.CreateDate.Date == end.Date);

That is not what I suggested. My suggestion was:

var start = DateTime.Now.Date;
var end = start.AddDays(1);

var todaysposts = postrepo.Where(p => p.CreateDate > start 
        && p.CreateDate < end);

I also explained that comparing the .Date properties won't work because the driver wil throw an NotSupportedException.

I kindly suggest to read my replies again, very carefully. It has all been said already.

You're welcome!