0
I’m trying to filter between dates in C# on a table in Mongodb. Expected results should be between 19/11/2020 and 20/11/2020.
This is my code on C#:
collection.Find<Admissao>(x => x.created_in >= startDate && x.created_in <= endDate.AddDays(1));
The code returns me 35 records.
If I run the same filter directly on Mongodb, this way:
db.getCollection('ContratosAdmissao').find({"created_in":{$gte:new ISODate("2020-11-19T00:00:00.000Z"),$lte:new ISODate("2020-11-21T00:00:00.000Z")}})
Returns me 41 records.
I do not understand why in C# does not return the same amount, I noticed that these 6 difference records have the time between 00:00:00 (midnight) and 02:00:00 (two hours).
If I change the time of these dates in Mongodb to 03:00:00, they will be filtered in C#.
Very likely the problem is this. However, I think it would be a good idea to [Dit] the answer and add options to solve it.
– Jéf Bueno
So it’s a little complicated to write a solution, because the problem itself is very abstract. For example he may have sent the date as 2020-11-18 21:00:00 but in Mongo was saved as UTC-0 so he set the time to 2020-11-19 00:00:00 so his query in C# is not wrong, it would be interesting to understand this point.
– Joao Victor de Paula Ramos