背景
我正在使用Spring-boot和MongoDB。
msgunpacked
findAllContactsForUser的输出:
public class User {
@DBRef
private List<Contact> contacts;
...
public class Contact {
@DBRef
private List<Booking> bookings;
...
public Contact(){}
public Contact(Booking booking){
this.bookings = new ArrayList<Booking>();
this.bookings.add(booking);
...
}
@Override
public List<Contact> findAllContactsForUser(String id) {
Query query = new Query().addCriteria(Criteria
.where("_id").is(id));
query.fields().include("contacts");
User user = mongoTemplate.findOne(query, User.class);
return user.getContacts();
}
问题:
我想要的结果:
[
{
"field":"value",
"bookings": [
"field":"value"
]
}
]
如何在findAllContactsForUser?
中排除查询中的预订关系答案 0 :(得分:0)
您只需在查询中排除字段contacts.bookings
:
@Override
public List<Contact> findAllContactsForUser(String id) {
Query query = new Query().addCriteria(Criteria.where("_id").is(id));
query.fields().exclude("contacts.bookings");
User user = mongoTemplate.findOne(query, User.class);
return user.getContacts();
}
顺便说一下,默认情况下不需要包含.include(contacts)
字段。