MongoDB фильтр данных Агрегация Spring загрузки - PullRequest
0 голосов
/ 06 февраля 2020

Привет всем, я хотел бы отфильтровать по бренду, а также по рейтингу Вот данные, которые я хочу отфильтровать

[
    {
        "name": "iPhone 6",
        "category": "High-Tech",
        "productId": "LhKiRGr6",
        "details": {
            "rating": "5",
            "stocks": "20",
            "price": "800000",
            "tags": [
                {
                    "tags1": "Apple",
                    "tags2": null,
                    "tags3": null
                }
            ],
            "brand": "Apple",
            "description": "Iphone X",
            "picture": [
                {
                    "picture1": "photo1",
                    "picture2": null,
                    "picture3": null,
                    "picture4": null,
                    "picture5": null
                }
            ],
            "thumbnails": [
                {
                    "thumbnail1": "thumbails 1",
                    "thumbnail2": null,
                    "thumbnail3": null,
                    "thumbnail4": null,
                    "thumbnail5": null
                }
            ]
        }
    },
    {
        "name": "iPhone X",
        "category": "High-Tech",
        "productId": "dgCvi8NJ",
        "details": {
            "rating": "5",
            "stocks": "20",
            "price": "800000",
            "tags": [
                {
                    "tags1": "Apple",
                    "tags2": null,
                    "tags3": null
                }
            ],
            "brand": "Apple",
            "description": "Iphone X",
            "picture": [
                {
                    "picture1": "photo1",
                    "picture2": null,
                    "picture3": null,
                    "picture4": null,
                    "picture5": null
                }
            ],
            "thumbnails": [
                {
                    "thumbnail1": "thumbails 1",
                    "thumbnail2": null,
                    "thumbnail3": null,
                    "thumbnail4": null,
                    "thumbnail5": null
                }
            ]
        }
    },
    {
        "name": "iPhone X",
        "category": "High-Tech",
        "productId": "4LvrygsV",
        "details": {
            "rating": "4",
            "stocks": "20",
            "price": "800000",
            "tags": [
                {
                    "tags1": "Apple",
                    "tags2": null,
                    "tags3": null
                }
            ],
            "brand": "Apple",
            "description": "Iphone X",
            "picture": [
                {
                    "picture1": "photo1",
                    "picture2": null,
                    "picture3": null,
                    "picture4": null,
                    "picture5": null
                }
            ],
            "thumbnails": [
                {
                    "thumbnail1": "thumbails 1",
                    "thumbnail2": null,
                    "thumbnail3": null,
                    "thumbnail4": null,
                    "thumbnail5": null
                }
            ]
        }
    },
    {
        "name": "iPhone XMAS",
        "category": "High-Tech",
        "productId": "aDPXF7Xq",
        "details": {
            "rating": "4",
            "stocks": "20",
            "price": "800000",
            "tags": [
                {
                    "tags1": "Apple",
                    "tags2": null,
                    "tags3": null
                }
            ],
            "brand": "Apple",
            "description": "Iphone X",
            "picture": [
                {
                    "picture1": "photo1",
                    "picture2": null,
                    "picture3": null,
                    "picture4": null,
                    "picture5": null
                }
            ],
            "thumbnails": [
                {
                    "thumbnail1": "thumbails 1",
                    "thumbnail2": null,
                    "thumbnail3": null,
                    "thumbnail4": null,
                    "thumbnail5": null
                }
            ]
        }
    },
    {
        "name": "iPhone XR",
        "category": "High-Tech",
        "productId": "cjjVqOBk",
        "details": {
            "rating": "2",
            "stocks": "20",
            "price": "800000",
            "tags": [
                {
                    "tags1": "Apple",
                    "tags2": null,
                    "tags3": null
                }
            ],
            "brand": "Apple",
            "description": "Iphone X",
            "picture": [
                {
                    "picture1": "photo1",
                    "picture2": null,
                    "picture3": null,
                    "picture4": null,
                    "picture5": null
                }
            ],
            "thumbnails": [
                {
                    "thumbnail1": "thumbails 1",
                    "thumbnail2": null,
                    "thumbnail3": null,
                    "thumbnail4": null,
                    "thumbnail5": null
                }
            ]
        }
    }
]

А вот код, который я настроил для фильтрации

Criteria criteria = new Criteria();

        final Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(

                        criteria.orOperator(
                                Criteria.where("name").regex(query, "i"),
                                Criteria.where("details.brand").regex(query, "i"),
                                Criteria.where("details.tags.tags1").regex(query, "i"),
                                Criteria.where("details.tags.tags2").regex(query, "i"),
                                Criteria.where("details.tags.tags3").regex(query, "i")

                        ).andOperator(
                                Criteria.where("details.brand").in(brand),
                                Criteria.where("details.rating").in(rating)

                        )

                ),
                Aggregation.skip(page * pageable.getPageSize()),
                Aggregation.limit(pageable.getPageSize())

        );
        List<Products> filter = mongoTemplate.aggregate(aggregation, "Product", Products.class).getMappedResults();
        return new PageImpl<Products>(filter, pageable, filter.size());
    }

Но фильтрация работает неправильно, я вам объясняю

Если я хочу иметь бренды, я должен также поставить рейтинг, который соответствует брендам

И если я хочу чтобы иметь рейтинги, я должен также поставить бренды, которые соответствуют рейтингам

Таким образом, вы видите мою проблему немного, наоборот

Теперь я хочу, чтобы все бренды, которые иметь рейтинг 5, например

Или снова все оценки всех продуктов, которые имеют рейтинг 4

Я действительно надеюсь, что вы понимаете, что я имею в виду, вся ваша помощь приветствуется, спасибо

...