Coder Social home page Coder Social logo

Comments (6)

RobThree avatar RobThree commented on June 12, 2024

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.

from mongorepository.

bhanu7755 avatar bhanu7755 commented on June 12, 2024

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.

from mongorepository.

RobThree avatar RobThree commented on June 12, 2024

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.

from mongorepository.

bhanu7755 avatar bhanu7755 commented on June 12, 2024

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

from mongorepository.

RobThree avatar RobThree commented on June 12, 2024

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.

from mongorepository.

RobThree avatar RobThree commented on June 12, 2024

You're welcome!

from mongorepository.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.