我是 Spring Boot 和 mongoDB 的新手。有映射的集合,我必须使用 join 来获取数据。 集合 1:
@Document("userResponses")
public class UserResponse {
private String moduleName;
private String subModule;
private int botResponseId;
private String botResponseType;
private HashMap<String,String> responseData;
private String sentAtInstant;
private String userId;
// setters and getters
}
集合 2:
@Document("questions")
public class Question implements ResponseData{
@Id
private int id;
private String text;
private String shortText;
private String textStyleClass;
private List<Option> options;
private Map<String,Object> metaInfo;
private Map<String,Object> optionsInfo;
private String insLogin;
private String updLogin;
private String insTs;
private String updTs;
//setters and getters
}
我的代码:
Criteria criteria = new Criteria();
MatchOperation match = Aggregation.match(criteria);
LookupOperation lookup = Aggregation.lookup("userResponses",
"botResponseId",
"_id",
"questions");
TypedAggregation<UserResponse> agg =Aggregation.newAggregation(UserResponse.class,match, lookup);
AggregationResults<UserResponse> res = mongoTemplate.aggregate(agg, UserResponse.class);
List<UserResponse> res1 = res.getMappedResults();
它只给我 userResponse 集合,不知何故它没有给我合并的集合,即 userResponse 和 questions 集合。