【问题标题】:Filetr items using mongotemplate - aggregation in spring boot使用 mongotemplate 过滤项目 - spring boot 中的聚合
【发布时间】: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


【解决方案1】:

我找到了解决方案,而不是我尝试聚合的简单条件查询。

@RequestMapping(value = "/getcashbackitems", method = RequestMethod.GET)
public List<ProductsObj> getAllCashbackItems() {
    Query query = new Query();
    query.addCriteria(Criteria.where("listOfPromotions.promotionName").is("cashback"));
    return mongoTemplate.find(query, ProductsObj.class);
}

感谢您的回复:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 2020-09-28
    • 2021-08-03
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多