我正在尝试使用Spring查询Mongo存储库并过滤数组子文档。我引用了how to filter array in subdocument with mongodb,但是想知道是否有一个更合适的或java结构化的方法来使用Spring。
我目前正在使用速记存储库接口表示法,但我正在使用未过滤的数组返回完整的文档。
PersonRepository.java
@Repository
public interface PersonRepository extends MongoRepository <Person, String> {
List<Person> findByAddressZipCode(@Param("zip") int zip);
}
Person.java
@Document
public class Person {
@Id
private String id;
private String firstName;
private String lastName;
private List<Address> address;
}
Address.java
public class Address {
private int zip;
}
示例输入
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"98765"
},{
"zip":"12345"
}]
}
预期输出
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"12345"
}]
}
实际输出
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"98765"
},{
"zip":"12345"
}]
}
答案 0 :(得分:4)
嗯,在Spring Data中,这类查询不是trivial
。
坏消息:
Spring Data Repository没有MongoDB Aggregation
的解决方案。因此,您无法在MongoRepository中实现任何方法,例如aggregateBy...
好消息:
Spring Data提供了MongoTemplate
类,允许您执行复杂查询,就像在标准MongoDB shell中一样。
因此,由于您只想要exclude
子文档与某些条件不匹配,我们需要定义聚合pipelines
。
我认为:
zip codes are Numeric (In your example is string)
And, to exclude subdocument, we filter by `zip`
There is no any other filter
MongoDB聚合将是:
db.person.aggregate([
{$unwind: "$address"},
{$match: {"address.zip": 12345}},
{$group: { _id: { "firstName":"$firstName", "lastName":"$lastName", _id:"$_id" }, address: { $push: "$address" } } },
{$project: {_id:0, "firstName":"$_id.firstName", "lastName":"$_id.lastName", "address": "$address"}}
])
如果所有过滤器都成功,我们得到:
[
{
"address" : [
{
"zip" : 12345
},
{
"zip" : 12345
}
],
"firstName" : "George",
"lastName" : "Washington"
}
]
<小时/> 现在,以Spring Data方式,您需要在项目中添加一些更改:
首先,找到您需要添加的mongo-config.xml
:
<!-- Define the mongoDbFactory with your database Name -->
<mongo:db-factory uri="mongodb://user:pass@localhost:27017/db"/>
<!-- Define the MongoTemplate -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
MongoTemplate
是Spring的MongoDB支持的核心类,提供与数据库交互的功能集。模板...
提供了域对象和 MongoDB文档之间的映射。 More info
其次,在@Service
课程中,添加以下代码以加载@PostConstruct
@Autowired
private MongoOperations mongoOperations;
...
public List<Person> findByAddressZipCode(int zip) {
List<AggregationOperation> list = new ArrayList<AggregationOperation>();
list.add(Aggregation.unwind("address"));
list.add(Aggregation.match(Criteria.where("address.zip").is(zip)));
list.add(Aggregation.group("firstName", "lastName").push("address").as("address"));
list.add(Aggregation.project("firstName", "lastName", "address"));
TypedAggregation<Person> agg = Aggregation.newAggregation(Person.class, list);
return mongoOperations.aggregate(agg, Person.class, Person.class).getMappedResults();
}
注意: Person
和Address
都应该有默认的空构造函数!