我有一个功能齐全的Spring Boot应用程序,可从mongodb进行读写。现在尝试实现查找,但是下面的代码无法找到“ project_following”集合,而仅返回“ project”集合的结果。当我交换集合名称时,结果将被反转,表明聚合本身是可行的,但无法找到“ LookupOperation.newLookup().from(“ project_following”))“”
中提到的集合它成功返回了ProjectJoinSample.class对象,但是没有返回来自project_following集合的数据。
收藏: project_following
@media print{
.notPrintable {
display: none !important;
}
}
项目
{
"_id" : "96e6c39c-be1f-4d22-9f05-957cb8d92c6b",
"count" : 1,
"userNames" : [
"DE10210"
]
}
配置:
{
"_id" : "96e6c39c-be1f-4d22-9f05-957cb8d92c6b",
"userName" : "AB10210",
"name" : "Super Fun Time Beer",
"description" : "Beer for fun"
}
application.properties:
@Value("${com.mongodb.collection.project:project}")
private String projectCollectionName;
@Bean
public String projectCollectionName() {
return this.projectCollectionName;
}
@Value("${com.mongodb.collection.project.following:project_following}")
private String projectFollowingCollectionName;
@Bean
public String projectFollowingCollectionName() {
return this.projectFollowingCollectionName;
}
代码:
com.mongodb.collection.project=project
com.mongodb.collection.project.following=project_following
预期结果:
LookupOperation lookupOperation = LookupOperation.newLookup()
.from("project_following"))
.localField("_id")
.foreignField("_id")
.as("project_join");
Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where("_id").is("96e6c39c-be1f-4d22-9f05-957cb8d92c6b")) , lookupOperation);
List<ProjectJoinSample> results = mongoTemplate.aggregate(aggregation, Project.class, ProjectJoinSample.class).getMappedResults();
实际结果:
{
"_id" : "96e6c39c-be1f-4d22-9f05-957cb8d92c6b",
"userName" : "AB10210",
"name" : "Super Fun Time Beer",
"description" : "Beer for fun!",
"project_join" : [
{
"_id" : "96e6c39c-be1f-4d22-9f05-957cb8d92c6b",
"count" : 1,
"userNames" : ["DE10210"]
}
]
}