【问题标题】:Query an array with regular expression in mongo repository在 mongo 存储库中使用正则表达式查询数组
【发布时间】:2020-09-05 20:36:09
【问题描述】:

以下查询不适用于 mongo 存储库 -

@Query(value = "{ 'items' : {$elemMatch : {'attributes' : {$all : [/^?0/i]}}}}")
List<MenuEntity> findMenuByItemAttribute(String attrName);

问题似乎出在我在注释中传递 ?0 的位置。我需要在某处传递引号吗?

db.menus.find({ 'items' : {$elemMatch : {'attributes' : {$all : [/^sav/i]}}}})

以上查询在 mongo shell 中返回结果

文档结构-

db.menus.findOne()
{
    "_id" : ObjectId("5cf25412326c3f4f26df039b"),
    "restaurantId" : "301728",
    "items" : [
            {
                    "itemId" : "CEBM4H41JR",
                    "name" : "Crun Chicken",
                    "imageUrl" : "",
                    "price" : 572,
                    "attributes" : [
                            "Tasty",
                            "Spicy"
                    ]
            },
            {
                    "itemId" : "53Q0XS3HPR",
                    "name" : "Devils Chicken",
                    "imageUrl" : "",
                    "price" : 595,
                    "attributes" : [
                            "Gravy",
                            "Salty"
                    ]
            }
}

【问题讨论】:

    标签: java mongodb spring-boot mongorepository


    【解决方案1】:

    不需要自己写mongo查询。您可以扩展 MongoRepository 并使用派生查询,只需在接口中定义您的方法,例如:

    List<MenuEntity> findByItemsAttributes(String attribute);
    

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 2012-10-15
      • 2017-07-12
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      相关资源
      最近更新 更多