Java MongoDB筛选器数据聚合Spring引导
大家好,我想就品牌和评级进行筛选 这是我要过滤的数据Java MongoDB筛选器数据聚合Spring引导,java,spring,mongodb,spring-boot,aggregation-framework,Java,Spring,Mongodb,Spring Boot,Aggregation Framework,大家好,我想就品牌和评级进行筛选 这是我要过滤的数据 [ { "name": "iPhone 6", "category": "High-Tech", "productId": "LhKiRGr6", "details": { "rating": "5", "stocks": "20", "price": "800000", "t
[
{
"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());
}
标准=新标准();
最终聚合=Aggregation.newAggregation(
聚合匹配(
标准操作器(
条件。其中(“名称”).regex(查询,“i”),
标准.where(“details.brand”).regex(查询,“i”),
条件.where(“details.tags.tags1”).regex(查询,“i”),
条件.where(“details.tags.tags2”).regex(查询,“i”),
条件.where(“details.tags.tags3”).regex(查询“i”)
).和接线员(
(品牌)中的标准,其中(“详细信息.品牌”),
(评级)中的标准,其中(“详细信息.评级”)
)
),
聚合.skip(page*pageable.getPageSize()),
聚合.limit(pageable.getPageSize())
);
List filter=mongoTemplate.aggregate(聚合,“产品”,Products.class).getMappedResults();
返回新的PageImpl(filter,pageable,filter.size());
}
但我向您解释,过滤工作不正常
如果我想拥有这些品牌,我还必须输入与这些品牌对应的评级
如果我想获得评级,我还必须列出与评级相对应的品牌
所以你有点明白我的问题,反之亦然
现在,我想要的是所有的品牌,比如评级为5的品牌
或者,所有评级为4的产品的所有评级
我真的希望你明白我的意思,欢迎你的帮助谢谢你变量
查询
,品牌
,和评级
在聚合中在哪里。匹配
获取它们的值?你想同时获取品牌和评级,但可以根据品牌或评级进行筛选吗?这就是你想要的吗?@sultania23 Hello我想要的是,当我筛选一个品牌时,我想要获得该品牌的所有评级brand@Joe你好,这是密码