0

below is my code -

MatchOperation match_Status_Count = new MatchOperation(
                Criteria.where("date1").gte("2019-3-25T17:34:24.734Z").andOperator(
                        Criteria.where("date1").lte("2019-11-25T17:34:24.734Z")
                                ));
        GroupOperation group_Status_Count = Aggregation.group("Status").count().as("Statuscount");
        Aggregation aggregate_Status_Count = Aggregation
                .newAggregation(match_Status_Count, group_Status_Count)
                .withOptions(new AggregationOptions(false, false,
                        new BasicDBObject(new Document().append("batchSize", 100000000000L))));
        ;
        AggregationResults<ARStructData> orderAggregate_Status_Count = mongoTemplate
                .aggregate(aggregate_Status_Count, D_AR_COLLECTION_NAME, ARStructData.class);

Since the date is stored as string ,hence it returning according to the string gte and lte. Appreciated if someone give me solution for it.

  • So you want to be able to say "give me docs between 2 dates where the input dates are actual date objects but the date in the doc a string"? – Buzz Moschetti Nov 25 '19 at 18:44
  • right now the input dates are not actual date object but string only. But modifying that also not working. And yes,the date in the doc is a string – sasanka talukdar Nov 26 '19 at 05:10

1 Answers1

0

Assuming your input dates are strings, you will want to convert them into java.util.Date e.g.

Date lowerDate = yourFavoriteCvtDateStringtoDate(lowerDateString);
Date upperDate = yourFavoriteCvtDateStringtoDate(upperDateString);

then create a query that leverages the $toDate function to turn the datestring in the doc into a real date that can be compared (in Javascript here for ease of viewing):

db.foo.aggregate([
{$addFields: {X: {$toDate: "$date1"} }}
,{$match: {$and: [ {"X": {$gt: lowerDate}}, {"X": {$lt: upperDate}} ] }}
                       ]);
Buzz Moschetti
  • 7,057
  • 3
  • 23
  • 33