查询MongoDB使用Micronaut数据嵌入文档

发布于 2025-01-21 18:59:36 字数 1777 浏览 5 评论 0 原文

使用Micronaut数据执行以下操作,

  1. 以根据类别ID和子类别更新子类别项目
  2. ID和子类别ID更新子类别项目 ID
  3. 基于类别ID和子删除子类别项 类别ID

I具有以下数据结构,

{
        "id": "625a8d1a23c8feb3234fbf28",
        "name": "Category 1",
        "description": "Category 1 description",
        "subCategory": [{
                "id": "625a8d6fe0e3cff6e8a3b795",
                "name": "Sub Category 1",
                "description": "Sub Category 1 description"
            },
            {
                "id": "625a8d782672979b1482f5ca",
                "name": "Sub Category 2",
                "description": "Sub Category 2 description"
            }
        ]
    }

Micronaut中有一个自定义查询构建器,如下所述 https://micronaut-projects.github.io/micronaut-data/latest/guide/#mongocustomqueries https://micronaut-projects.github.io/micronaut-data/micronaut-data/latest/pguide/#mongocriteriaexecutequery

@Introspected
public record SubCategorySpecification() {
    public static PredicateSpecification<Category> categoryAndSubCategoryIdEquals(ObjectId categoryId, ObjectId subCategoryId) {
        return (root,criteriaBuilder) -> {
            criteriaBuilder.equal(root.get("id"), categoryId);
            return criteriaBuilder.and(criteriaBuilder.equal(root.get("subCategory.id"), subCategoryId));
        };
    }
}

我尝试了上述代码异常AS 无法在不存在的属性上查询实体[类别]:subcategory.id ,不确定如何使用 @ mongofindquery 使用自定义查询编写自定义查询。

Using Micronaut data to perform the below operations

  1. Find the item with id and subcategory id
  2. Update the subcategory item based on category id and sub-category
    id
  3. Delete the subcategory item based on category id and sub
    category id

I have the below data structure

{
        "id": "625a8d1a23c8feb3234fbf28",
        "name": "Category 1",
        "description": "Category 1 description",
        "subCategory": [{
                "id": "625a8d6fe0e3cff6e8a3b795",
                "name": "Sub Category 1",
                "description": "Sub Category 1 description"
            },
            {
                "id": "625a8d782672979b1482f5ca",
                "name": "Sub Category 2",
                "description": "Sub Category 2 description"
            }
        ]
    }

There is a custom query builder in micronaut as describe here https://micronaut-projects.github.io/micronaut-data/latest/guide/#mongoCustomQueries and https://micronaut-projects.github.io/micronaut-data/latest/guide/#mongoCriteriaExecuteQuery

@Introspected
public record SubCategorySpecification() {
    public static PredicateSpecification<Category> categoryAndSubCategoryIdEquals(ObjectId categoryId, ObjectId subCategoryId) {
        return (root,criteriaBuilder) -> {
            criteriaBuilder.equal(root.get("id"), categoryId);
            return criteriaBuilder.and(criteriaBuilder.equal(root.get("subCategory.id"), subCategoryId));
        };
    }
}

I tried the above code and got an exception as Cannot query entity [Category] on non-existent property: subCategory.id and not sure how to write the custom query using @ MongoFindQuery

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2025-01-28 18:59:36

您可能需要将子类别定义为嵌入式以正确识别。

可以定义自定义查询:

   @MongoFindQuery("{ $and: [{\"_id\": :categoryId}, {\"subCategory.id\": :subCategoryId}]")
   List<Category> customFind(ObjectId categoryId, ObjectId subCategoryId);

以类似的方式,您可以使用 @mongoupdatequery @mongodeletequery
有关语法,请查看官方MongoDB文档。

You might need to have the subcategory defined as embedded to be properly recognized.

It's possible to define a custom query:

   @MongoFindQuery("{ $and: [{\"_id\": :categoryId}, {\"subCategory.id\": :subCategoryId}]")
   List<Category> customFind(ObjectId categoryId, ObjectId subCategoryId);

In a similar way, you can use @MongoUpdateQuery and @MongoDeleteQuery.
For the syntax check official MongoDB documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文