1

In my document, the date field is a string "date": "01/08/2018". How can I convert it to Date object in Spring MongoDB to able to use comparator operators like "gte, lte" to other date object.

I tried:

project()

   .and(DateOperators.DateFromString.fromStringOf("date")).as("date")

It just works with fields whose format is yyyy-MM-dd, and of course I ended up with:

'Error parsing date string '26/10/2016'; 0: Unexpected character '2'

How can I pass the pattern or Is there another way to get the my goal?

1 Answer 1

2

The format option has been added to $dateFromString in MongoDB Version 4 and is currently missing in Spring Data MongoDB. I've opened DATAMONGO-2047 to address the issue.
Meanwhile you may try the following to add it manually.

project().and(context -> {

    Document doc = DateFromString.fromStringOf("date").toDocument(context);
    doc.get("$dateFromString", Document.class).put("format", "dd/mm/yyyy");
    return doc;
}).as("date");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.