2016-07-23 156 views
1

當使用嵌入式mongo文檔我試圖展開數組,但我收到異常像org.springframework.data.mapping.model.MappingInstantiationException:無法實例化java.util .List使用帶參數的構造函數NO_CONSTRUCTOR。我就是寫的查詢,Mongo數據庫java unwind操作聚合查詢拋出異常

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 
     } 
] 
} 
+0

你能寫代碼在哪裏調用聚合方法嗎? –

+0

@Andriy Simonov:我從另一個類中調用聚合函數,只有一種方法,我將使用動態用戶配置文件Id調用該方法。但是,在這裏我硬編碼的用戶配置文件ID爲800. – Karthik

回答

1

如果聚合的結果是這樣的

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

日誌對象的名單,然後接收方的基數是不正確。它必須只是收件人而不是列表,因爲展開收件人字段後會保存單個文檔。

Logs.java 
    private String id; 
    private String userId; 
    private String conversationId; 
    private Date createdDate; 
    private Recipients recipients; <-- 
+0

但是,如果我這樣做,我可以只保存一個收件人嗎?我想要一個收件人數組保存爲單個記錄。 – Karthik

+1

然後你需要兩個類。記錄通常的操作,如保存和讀取,以及聚合結果的附加類。例如,您可以將其稱爲LogsAggregationResult。 –

+0

如何查詢另一個表,以獲得與我存儲我的記錄的表不同的聚合結果。你認爲它會起作用嗎? – Karthik