Mongo db java unwind操作在聚合查询中抛出异常

时间:2016-07-23 07:31:51

标签: java mongodb aggregation-framework spring-mongodb

使用嵌入式mongo文档时,我试图解开数组,但是我遇到了像org.springframework.data.mapping.model.MappingInstantiationException的异常:无法使用带参数的构造函数NO_CONSTRUCTOR实例化java.util.List。我写的查询是

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;

数据集

{
"_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
        }
]
 }

1 个答案:

答案 0 :(得分:1)

如果聚合的结果是像这样的Logs对象的列表

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

然后收件人的基数不正确。它必须只是一个收件人而不是List,因为在展开后,收件人字段包含一个文档。

Logs.java
  private String id;
  private String userId;
  private String conversationId;
  private Date createdDate;
  private Recipients recipients; <--
相关问题