Home > Software design >  Convert query with ISODate
Convert query with ISODate

Time:12-11

I have this MongoDB query:

db.Certificates.countDocuments(
  {"vaccination.somministration_date":ISODate("2021-08-04")}
)

and I want to perform it also in my java application, so I tried this code:

Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2021-08-04");
Bson query = eq("vaccination.somministration_date", date);
long count = collection.countDocuments(query);

But the results I get are different (from mongo I have 1 as result, while having 0 from java)

CodePudding user response:

MongoDB stores datetimes in GMT 0. Your first query in Javascript is calling ISODate() which always produces a datetime with GMT 0 ("Z"ulu) time offset. It is likely that your Java exec environment in calling parse() is taking local time offset into account and creating a slightly different datetime.

  • Related