Date filter problem in C# (Mongodb)

Asked

Viewed 103 times

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

inserir a descrição da imagem aqui

If I change the time of these dates in Mongodb to 03:00:00, they will be filtered in C#.

1 answer

0

You have to analyze that Mongodb has Datetime with UTC-0 probably when this record comes to your application it is coming with UTC-3 which is UTC from Brazil.

So actually this record on Mongodb when it comes to C# is subtracting three hours so they stop being Day 19 and become Day 18. That would answer because when the time is greater than 3 hours he can filter.

  • 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.

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.