Spring Boot数据和MongoDB-筛选子文档数组查询

我正在尝试使用Spring查询Mongo存储库并过滤数组子文档。我已经参考过如何使用mongodb过滤子文档中的数组,但是想知道是否有更合适的方法或Java结构化方法来使用Spring。

我当前正在使用速记存储库接口符号,但是我正在获取未过滤数组的完整文档。

@Repository

public interface PersonRepository extends MongoRepository <Person, String> {

List<Person> findByAddressZipCode(@Param("zip") int zip);

}

@Document

public class Person {

@Id

private String id;

private String firstName;

private String lastName;

private List<Address> address;

}

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"

}]

}

回答:

好吧,在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支持的核心类,提供与数据库交互的功能集。该模板...提供了 和

之间的映射。更多信息

其次,在您的@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应该有默认的空构造!

以上是 Spring Boot数据和MongoDB-筛选子文档数组查询 的全部内容, 来源链接: utcz.com/qa/429424.html

回到顶部