首页 > 解决方案 > JPA 中的 MongoDB 本机查询

问题描述

我想做的是编写方法,当给定一个 imageID 和 AnnotationID 时,它会从特定 Image 中的注释数组中删除注释。

我的数据结构看起来或多或少是这样的:

IMAGE:
{
ID,
name,
annotations{
    id
    ...
    }
}

我能够通过实际这样做(获取图像,更新列表,然后更新图像)来实现它。

Image image = imageRepository.readImageByName(name);
        Annotation[] annotations = image.getAnnotations();
        List<Annotation> annotationsToSave = new ArrayList<>();
        for (Annotation annotation:annotations){
            if(!annotation.get_id().equals(_id)){
                annotationsToSave.add(annotation);
            }
        }

        Annotation[] annotationsArrayToSave = 
        annotationsToSave.toArray(Annotation[]::new);

        image.setAnnotations(annotationsArrayToSave);
        imageRepository.save(image);
    }

我的问题是。有什么办法可以把它放在本机查询或 MongoTemplate 到 mongodb 中,实际上我在我的代码中做了同样的事情,但是在服务器端?

感谢您的回答。

标签: javamongodbjpa

解决方案


推荐阅读