Java Spring数据MongoDB中的内部联接集合
如何在Spring Mongo中实现内部连接? 愚蠢的示例,例如,它实际上是不正确的,我只想显示多对多关系:Java Spring数据MongoDB中的内部联接集合,java,spring,mongodb,spring-boot,spring-data-mongodb,Java,Spring,Mongodb,Spring Boot,Spring Data Mongodb,如何在Spring Mongo中实现内部连接? 愚蠢的示例,例如,它实际上是不正确的,我只想显示多对多关系: @Document(collection = "people") public class Person { @Id private String id; private String name; private String petId; // Getters, setters, constructors and etc. } @Do
@Document(collection = "people")
public class Person {
@Id
private String id;
private String name;
private String petId;
// Getters, setters, constructors and etc.
}
@Document(collection = "pets")
public class Pet {
@Id
private String id;
private String name;
private PetType petType; // Dog, Cat etc.
// Getters, setters, constructors and etc.
}
如果我想找到所有属于约翰·史密斯的狗,我该怎么做?我需要这样的查询:
SELECT
pt.*
FROM
pets AS pt INNER JOIN people AS pe ON (pt.id = pe.petId)
WHERE
pt.petType = ${input_petType}
AND pe.name = ${input_name}
这意味着我在collectionPet
和collectionPerson
中有两个条件:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.List;
public interface PetRepository extends MongoRepository<Pet, String>, PetCustomRepository {
}
public interface PetCustomRepository {
List<Pet> findAllByPetTypeAndPersonName(PetType type, String personName, Pageable pageable);
}
public class PetCustomRepositoryImpl implements PetCustomRepository {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<Pet> findAllByPetTypeAndPersonName(PetType petType, String personName, Pageable pageable) {
LookupOperation lookup = LookupOperation.newLookup()
.from("people")
.localField("_id")
.foreignField("petId")
.as("join_people");
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("petType").is(petType)),
lookup,
Aggregation.match(Criteria.where("join_people.name").is(personName)),
Aggregation.skip(pageable.getPageNumber() * pageable.getPageSize()),
Aggregation.limit(pageable.getPageSize()));
return mongoTemplate.aggregate(aggregation, Pet.class, Pet.class).getMappedResults();
}
}
import org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.data.domain.Pageable;
导入org.springframework.data.mongodb.core.MongoTemplate;
导入org.springframework.data.mongodb.core.aggregation.aggregation;
导入org.springframework.data.mongodb.core.aggregation.LookupOperation;
导入org.springframework.data.mongodb.core.query.Criteria;
导入org.springframework.data.mongodb.core.query.query;
导入java.util.List;
公共接口PetRepository扩展了MongoRepository,PetCustomRepository{
}
公共接口存储库{
列出FindAllTypeTType和personName(PetType类型,字符串personName,可分页);
}
公共类PetCustomRepositoryImpl实现了PetCustomRepository{
@自动连线
私有MongoTemplate MongoTemplate;
@凌驾
公共列表FindAllTypeTypeAndPersonName(PetType PetType,String personName,Pageable Pageable){
LookupOperation lookup=LookupOperation.newLookup()
.来自(“人”)
.localField(“\u id”)
.foreignField(“petId”)
.作为(“加入人民”);
Aggregation=Aggregation.newAggregation(
Aggregation.match(标准,其中(“petType”)为(petType)),
查找,
Aggregation.match(Criteria.where(“join_people.name”)是(personName)),
聚合.skip(pageable.getPageNumber()*pageable.getPageSize()),
聚合.limit(pageable.getPageSize());
返回mongoTemplate.aggregate(聚合,Pet.class,Pet.class).getMappedResults();
}
}
findallTypeTTypeandPersonName()
方法返回空列表。我做错了什么?您的可分页设备有问题。尝试删除它,它将返回结果您尝试过吗
LookupOperation lookup = LookupOperation.newLookup()
.from("people")
.localField("id")
.foreignField("petId")
.as("join_people");
你能详细说明一下吗?不清楚你的意思。