이 있는 경우 문서로 정의
[
{
"title": "title",
"tags": [ "cool", "amazing", "funny" ]
},
{
"title": "another title",
"tags": [ "nice", "amazing", "funny" ]
}
]
고 싶를 쿼리할 수 있으로 MongoTemplate 순서로 목록을 전달하는 값은 다음과 같["멋진","amazing"]고 반환에서 첫 번째는 컬렉션,위의 두 번째입니다. 을 위해 무엇을 의미를 달성, $에서는 조건하지 않는 것 충분하다. 내가 함께 노력은 $모든 조건에서 몽고 콘솔 그것은 작품으로 필요하지만,나의 코드가 작동하지 않습니다.에 대한 쿼리를 정교하다. 과 $에서 운영자는 내가 빠르고,대신 합니다. 내 방법에 저장소(에 대한 다른 이유가 있을 집계 아래로):
public Page<MyDocument> findByProperties(String title, List<ObjectId> tags, Pageable page) {
final List<Criteria> criteria = new ArrayList<>();
Aggregation aggregation = null;
if (title != null && !title.isEmpty()) {
criteria.add(Criteria.where("title").is(title));
}
if (tags != null && !tags.isEmpty()) {
criteria.add(Criteria.where("MyBook.tags").all(tags));
}
if (!criteria.isEmpty()) {
aggregation = Aggregation.newAggregation(
Aggregation.lookup("from_collection", "_id", "idParent", "MyBook"),
Aggregation.unwind("MyBook"),
Aggregation.match(new Criteria().andOperator(criteria.toArray(new Criteria[0]))),
Aggregation.skip(page.getOffset()),
Aggregation.limit(page.getPageSize()),
Aggregation.sort(page.getSort())
);
} else {
aggregation = Aggregation.newAggregation(
Aggregation.lookup("from_collection", "_id", "idParent", "MyBook"),
Aggregation.unwind("MyBook"),
Aggregation.skip(page.getOffset()),
Aggregation.limit(page.getPageSize()),
Aggregation.sort(page.getSort())
);
}
List<MyDocument> results = mongoTemplate.aggregate(aggregation, "my_document", MyDocument.class).getMappedResults();
return PageableExecutionUtils.getPage(results, page,
() -> (long)results.size());
}
에서 찾는 이 대답했으로
criteria.add(Criteria.where("MyBook.tags").in(tags).all(tags));
하지만 아무것도 변경,쿼리에서 영원히지 않으로 예상되는 출력된다. 어떤 아이디어를 주세요? 감사합니다!