Comments (6)
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.
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.
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.
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.
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 anNotSupportedException
.
I kindly suggest to read my replies again, very carefully. It has all been said already.
from mongorepository.
You're welcome!
from mongorepository.
Related Issues (20)
- Delete not working as expected HOT 2
- how to insert data into collection used DBRef HOT 1
- Is this package supports MongoDB version 2.2.3? HOT 1
- when update to driver 2.2? HOT 1
- Referencing external documents, opposed to embedding. HOT 3
- Release on Nuget ? HOT 1
- how to search mongodb for list of ids HOT 1
- mongorepository update specific fields or all fields HOT 1
- Using the latest MongoDB.Driver ? HOT 1
- Using a shared MongoClient or MongoDatabase instance HOT 5
- Any possibility of an update to latest drivers? HOT 1
- Certain value types serializing as binary? HOT 3
- new mongoCsharpDriver HOT 1
- Version 2
- Castle Windsor registration HOT 1
- Migration to .NET Core / .NET Standard HOT 5
- MongoRepositary with latest MongoDB Driver HOT 1
- Support for 2.4.4 driver HOT 1
- Unable to get individual properties from collection document after inheriting from Entity HOT 8
- can not use GetSingle function in MongoRepository.Net45, Version=1.6.11.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongorepository.