2

When working with embedded mongo document I am trying to unwind the array but I am getting exception like org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments. The query what I wrote is,

Aggregation agg = newAggregation(
        unwind("recipients"),
 match(Criteria.where("recipients.userId").is("800").andOperator(Criteria.where("recipients.status").is(false)
                )));
  Logs.java
 private String id;
private String userId;
private String conversationId;
 private Date createdDate;
private List<Recipients> recipients;

Recipients.java

 private String userId;
 private boolean status;

Data set

{
"_id" : ObjectId("579099e6000fda45000c0054"),
"userId" : "800",
"conversationId" : "57907e5f000fda45000c004b",
"createdDate" : ISODate("2016-07-21T09:46:14.602Z"),
"recipients" : [
        {
                "userId" : "800",
                "status" : false
        },
        {
                "userId" : "900",
                "status" : false
        }
]
  }
 {
"_id" : ObjectId("579099e9000fda45000c0055"),
"userId" : "530a7998-ba3f-4366-8d21-bb1ca688cfdb",
"conversationId" : "57907e5f000fda45000c004b",
"createdDate" : ISODate("2016-07-21T09:46:17.915Z"),
"recipients" : [
        {
                "userId" : "800",
                "status" : true
        },
        {
                "userId" : "900",
                "status" : false
        }
]
 }
{
"_id" : ObjectId("5790adda000fda2444d6ccdf"),
"userId" : "530a7998-ba3f-4366-8d21-bb1ca688cfdb",
"conversationId" : "578df6cf000fda2640b77c45",
"createdDate" : ISODate("2016-07-21T11:11:22.522Z"),
"recipients" : [
        {
                "userId" : "800",
                "status" : false
        },
        {
                "userId" : "530a7998-ba3f-4366-8d21-bb1ca688cfdb",
                "status" : true
        }
 ]
 }
 {
"_id" : ObjectId("5790adde000fda2444d6cce0"),
"userId" : "530a7998-ba3f-4366-8d21-bb1ca688cfdb",
"conversationId" : "578df6cf000fda2640b77c45",
"createdDate" : ISODate("2016-07-21T11:11:26.479Z"),
"recipients" : [
        {
                "userId" : "800",
                "status" : false
        },
        {
                "userId" : "530a7998-ba3f-4366-8d21-bb1ca688cfdb",
                "status" : true
        }
]
 }
Karthik
  • 470
  • 8
  • 25
  • Can you write the code where you call aggregation method? – Andriy Simonov Jul 23 '16 at 09:43
  • @Andriy Simonov : From another class I am calling, The aggregate function is in one method , I will call that method with dynamic user profile Id. But, here I have hardcoded the user profile id as 800. – Karthik Jul 23 '16 at 09:53

1 Answers1

1

If the result of your aggregation is a list of Logs objects like this

AggregationResults<Logs> results = mongoOps.aggregate(agg, "logs", Logs.class);

then cardinality of recipients is incorrect. It must to be just a Recipients as opposed to List because after unwinding the recipients field holds a single document.

Logs.java
  private String id;
  private String userId;
  private String conversationId;
  private Date createdDate;
  private Recipients recipients; <--
Andriy Simonov
  • 1,276
  • 1
  • 11
  • 19
  • But, if I do like this I can have only save one recipient right? I want a array of recipient to saved for a single record. – Karthik Jul 23 '16 at 11:30
  • 1
    Then you need two classes. Logs for usual operations like saving and reading and an additional class for the aggregation result. You can call it LogsAggregationResult for example. – Andriy Simonov Jul 23 '16 at 12:15
  • How can I query a another table for aggregation results that is different from table where I stored my records. Do you think it will work. – Karthik Jul 24 '16 at 05:15