3

In the normal Mongo shell, it is possible to perform the following step during an aggregation to create an ISODate object from a Long timestamp, and therefore be able to use the various date helper functions ($year,$month, etc):

{
    $project:{
        'date': {
            $add: [
                new Date(0),
                    {
                    $multiply:[
                        '$seconds_timestamp_field',
                        1000
                    ]
                }
            ]
        }
}}

Is something similar possible using Spring Data? The plus() method does not appear to support a Java Date object as an argument. Thanks for your help.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
Joseph Blair
  • 1,385
  • 2
  • 12
  • 25

1 Answers1

0

In Java you can use the Joda library to manipulate the data, preferably using the Joda lib > org.joda.time.DateTime.

Convert the DateTime object to an ISO using toDateTimeISO() method on the DateTime class.

Here is an example of the usage. but do use this once the question asked is resolved.

Query query = new Query();
        DateTime dateTest = new DateTime(year, 1, 1, 0, 0);

        query.addCriteria(Criteria.where("monthYear").gte(dateTest.toDateTimeISO()));
        System.out.println(query.toString());
        List<Spendings> spendingsList = dbConfig.getMongoOperations().find(query, Spendings.class, collectionName);
Community
  • 1
  • 1
Srikanta
  • 1,145
  • 2
  • 12
  • 22
  • Unfortunately this doesn't really cover what I'm trying to do. I'm trying to use the $year/$month/etc operators available in the aggregation framework, but these are only available for ISODate objects, whereas I just have timestamps stored in Mongo (as a number). What I'm looking to do is convert the timestamp to an ISODate as one of the aggregation steps. – Joseph Blair Dec 01 '15 at 16:22
  • @JosephBlair If you have the timestamp in the epoch format then you can use the DateTime to convert it to the ISODate format – Srikanta Dec 02 '15 at 10:57
  • Late to this, but https://stackoverflow.com/questions/32789189/aggregation-project-group-by-extracted-day-month-and-year-with-spring-data has an explanation and workaround for this. – David Wu May 27 '19 at 20:06