【发布时间】:2019-04-12 21:57:52
【问题描述】:
我正在尝试使用过滤器根据“返现”优惠过滤产品,但它不起作用。提前致谢
我的sprintboot方法是:
public List<ProductsObj> getAllCashbackItems() {
Query query = new Query();
query.addCriteria(Criteria.where("listOfPromotions").in("cashback"));
Aggregation aggregation = newAggregation(
project().and(filter("listOfPromotions")
.as("item")
.by(valueOf(
"item.promotionName")
.equalToValue(
"cashback")))
.as("listOfPromotions")
);
return mongoTemplate.aggregate(aggregation, "productsObj", ProductsObj.class).getMappedResults();
}
我的收藏看起来很像:
[
{
"_id": "5be29135f590eb09a079951a",
"name": "tennis ball",
"price": "1500",
"category": "sports",
"listOfPromotions": [
{
"promotionName": "cashback",
"percentage": 12
}
]
},
{
"_id": "5be29142f590eb09a079951b",
"name": "volley ball",
"price": "600",
"category": "sports",
"listOfPromotions": [
{
"promotionName": "dham dhamaal",
"percentage": 6
}
]
},
{
"_id": "5be2914ef590eb09a079951c",
"name": "Dinning table",
"price": "15000",
"category": "furnitures",
"listOfPromotions": [
{
"promotionName": "cashback",
"percentage": 6
},
{
"promotionName": "dham dhamaal",
"percentage": 10
}
]
}
]
我的 ProductsObj POJO 类是:-
@Document(collection = "productsObj")
public class ProductsObj {
@Id
public ObjectId _id;
@Indexed
public String name;
public String price;
public String category;
public List<Promotion> listOfPromotions;
//constructors
//getters and setters
}
我正在尝试根据上述条件(即现金返还优惠)过滤对象,在 Promotions 实体类中包含promotionName 和百分比。
【问题讨论】:
-
你使用的是哪个 ORM?
-
mongodb 原始查询对你有用吗?
-
哪一部分不工作?您的查询看起来正确。你能告诉我们 productobj pojo 类吗?
-
我已经更新了 pojo @Veeram
-
我正在使用 springdata @alok
标签: mongodb spring-boot nosql aggregation-framework mongotemplate