1

I'm struggling with translating this mongoDB query in Spring Data MongoDB:

db.applicates.aggregate(
   [
   {
      "$match": {
        "claimantCategory": 1
      }
    },
      {
        $group : {
           _id : { month: { $month: "$claimDate" }, day: { $dayOfMonth: "$claimDate" }, year: { $year: "$claimDate" } },
       count: { $sum: 1 }
    }
  }
   ]
)

I've tryed this:

TypedAggregation<Applicate> aggregation = newAggregation(Applicate.class,
            match(Criteria.where("claimantCategory").is(claimantCategory)),
            project("claimDate")
                    .andExpression("dayOfMonth("+"\""+"claimDate"+"\""+")").as("day")
                    .andExpression("month(claimDate)").as("month")
                    .andExpression("year(claimDate)").as("year"),
            group(fields().and("day").and("month").and("year"))
                    .count().as("count"));
AggregationResults<ClaimsAggregator> groupResults = mongoOperations.aggregate(aggregation, ClaimsAggregator.class);

But it fails with exception : can't convert from BSON type String to Date

My Applicate.class:

public class Applicate {

@Id
private String id;

private int claimId;

@DateTimeFormat (iso = DateTimeFormat.ISO.DATE)
private Date claimDate;

private int codeOfForm;
private String claimStatus;
private String fio;
private int claimantCategory;
private int serviceCode;
private String subserviceName;
private String departmentName;
//getter,setters, constructor
}

ClaimsAggregator.class:

public class ClaimsAggregator {
private String claimDate;
private int count;
....
}
3
  • Replace the line .andExpression("dayOfMonth("+"\""+"claimDate"+"\""+")").as("day") with this .andExpression("dayOfMonth(claimDate)").as("day"). Commented Nov 23, 2015 at 12:56
  • @chridam, thanks for suggestion. Just tested that and now it returns null in date field. What may cause that? Commented Nov 23, 2015 at 13:02
  • Not too sure, might be some documents which don't have the claimDate field? Commented Nov 23, 2015 at 13:04

1 Answer 1

3

I've found an answer! as @chridam suggested, u need to replace the line

.andExpression("dayOfMonth("+"\""+"claimDate"+"\""+")").as("day")

with this

.andExpression("dayOfMonth(claimDate)").as("day")

Now you need to add

.first("claimDate").as("claimDate")

to line

group(fields().and("day").and("month").and("year"))

Final code:

TypedAggregation<Applicate> aggregation = newAggregation(Applicate.class,
            match(Criteria.where("claimantCategory").is(claimantCategory)),
            project("claimDate")
                    .andExpression("dayOfMonth(claimDate)").as("day")
                    .andExpression("month(claimDate)").as("month")
                    .andExpression("year(claimDate)").as("year"),
            group(fields().and("day").and("month").and("year")).first("claimDate").as("claimDate")
                    .count().as("count"));
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.