Vipul Prakash
Vipul Prakash

Reputation: 91

How to compare only date of dateTime while filtering from mongo in c#?

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?

Upvotes: 2

Views: 944

Answers (1)

Vic F
Vic F

Reputation: 1459

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.

Upvotes: 2

Related Questions