2

My date time property defined in my model class named Recommendation is

public DateTime? DueDate { get; set; } 

I am trying to filter and retrieve data from mongo where the field DueDate matches the current date only not including time.

I made a filter expression for querying on mongo.

var dateFilter = Builders<Recommendation>.Filter.Eq(x => x.DueDate, DateTime.Now) 

DateTime.Now also returns the date with a timestamp.

Also in mongo, the DueDate field value is 2022-12-08T05:00:00.000+00:00

How can I modify the filter query above to filter and fetch records from mongo based on date only and not timestamp?

1 Answer 1

2

You could convert them to strings:

var dateFilter = Builders<Recommendation>.Filter.Eq(x => x.DueDate.ToString("yyyyMMdd"), DateTime.Now.ToString("yyyyMMdd"); 

No promises on the performance of this.

Sign up to request clarification or add additional context in comments.

2 Comments

When applying on x.DueDate.ToString("yyyyMMdd") it is giving error that "No overload for method ToString takes 1 argument". Also, DueDate is nullable as well -DateTime? . It is working fine on DateTime.Now though but only gives an error on DueDate. It's only accepting ToString() without arguments on DueDate part
If they are Nullable, it might be: x.DueDate.GetValueOrDefault().ToString("yyyyMMdd")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.