MongoTemplate 쿼리를 배열과 일치하는 이상한 값을 나는 전달

0

질문

이 있는 경우 문서로 정의

[
  {
    "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));

하지만 아무것도 변경,쿼리에서 영원히지 않으로 예상되는 출력된다. 어떤 아이디어를 주세요? 감사합니다!

1

최고의 응답

0

을 찾는 경우에는 태그는 배열을 포함한 모든 회원의 아이 배열을 사용할 수 있습 우는 소리,당신이 그것을 필요로 하는 경우에 집계 파이프라인 사용하여 두 번째입니다.

쿼리에서 당신은 더 많은 단계이며,더 많은 코드고 싶은 경우 요청하는 경우 당신은 더 많은 줄 수 있는 예 데이터 및 예상 출력에서 JSON,그리고 당신이 원하는 무엇,그래서 사람들이 도움이 될 수 있습니다.

Query1

  • $all 연산자

테스트 코드기

db.collection.find({"tags": {"$all": ["cool","amazing"]}})

Query2

  • 집계된 솔루션을 설정 차이

테스트 코드기

aggregate(
[{"$match": 
    {"$expr": 
      {"$eq": [{"$setDifference": [["cool", "amazing"], "$tags"]}, []]}}}])
2021-11-23 17:19:23

안녕 Takis 를 위한 감사합니다. 내가 무엇을 해야 할 것입 Java,지 않을 통해 몽고 콘솔 그것은 이미 작동합니다. 와 MongoTemplate 내가 사용하는 프로젝션,그리고 내 경우에는 오히려 스틱으로 집계. 위 예제 코드는,나만에 가입해야 두 가지 컬렉션(집계)쿼리에는 배열에 존재"의 mybook"컬렉션 참여로
Barbi

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................