使用Spring Data MongoDB在MongoDB中查找包含嵌入式数组中元素的子列表的实体

相互依赖

我有一个具有标签列表的MongoDB实体:

@Document
@TypeAlias("project")
data class Project(@Id var id: String?,
                   val name: String,
                   val tags: MutableList<Tag>)

我想寻找包含标签子集的Project。

例如,给定这些proyects:

projectService.save(Project(null, "someProject11",  
    mutableListOf(Tag("area","IT"), Tag("department","Architecture"))))

projectService.save(Project(null, "someProject12", 
    mutableListOf(Tag("area","IT"), Tag("department","HR"))))

如果我按以下方式定义存储库:

interface ProjectRepository : ReactiveCrudRepository<Project, String> {
    fun findByTags(tags : List<Tag>): Flux<Project>
}

寻找子集不起作用,例如:的项目Tag("area","IT")应返回两个结果,但实际上返回0。底层的mongo查询为:

{"find": "project", "filter": {"tags": [{"key": "area", "value": "IT"}]}

它只能通过列表的完整内容listOf(Tag("area","IT"), Tag("department","Architecture"))

{"find": "project", "filter": {"tags": [{"key": "area", "value": "IT"},{"key": "department", "value": "Architecture"}]}

如何查询包含列表子集的实体?

相互依赖

使用Criteria$elemMatch运算符解决

interface CustomProjectRepository {
    fun findByTags(tags: List<Tag>): Flux<Project>
}

class CustomProjectRepositoryImpl(private val mongoTemplate: ReactiveMongoTemplate) : CustomProjectRepository {
    override fun findByTags(tags: List<Tag>): Flux<Project> {
        val query = Query()
        val criterias = mutableListOf<Criteria>()
        tags.forEach {
            criterias.add(Criteria.where("tags").elemMatch(Criteria.where("key").`is`(it.key).and("value").`is`(it.value)))
        }
        query.addCriteria(Criteria().andOperator(* criterias.toTypedArray()))
        return mongoTemplate.find(query, Project::class.java)
    }
}

interface ProjectRepository : ReactiveCrudRepository<Project, String>, CustomProjectRepository {

    fun findByClientIdAndId(clientId: String, id: String): Mono<Project>
    fun findByClientId(clientId: String): Flux<Project>
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

查询Spring Data MongoDB中嵌入式文档的数组

如何使用Spring Data MongoDB Repository以greatThan查询嵌入式文档?

如何使用Spring Data MongoDB MongoTemplate插入嵌入式文档

Spring Data MongoDB-嵌入式文档在其他文档中作为参考

spring-data-mongodb:findAll()带有输入文档列表和嵌入式DBRef文档的搜索参数

使用Spring Data通过实体ID从实体获取嵌入式对象

如何使用id mongodb在嵌入式文档数组中查找数据?

在MongoDB中搜索嵌入式数组(使用Java)

嵌入式实体的Spring Data Rest投影

Spring Data JPA通过嵌入式对象属性查找

如何检索和删除嵌入式文档Spring Data MongoDB

Spring Data JPA通过嵌入式密钥中的多个字段查找

使用Spring Data在$ project MongoDB中的$ filter

Spring Data:嵌入式/非嵌入式?

Spring Data MongoDB嵌入查询

使用Spring Boot的MongoDB嵌入式文档

使用Spring Data和MongoDB查找具有最新日期的实体

使用Presto查询MongoDB嵌入式/嵌套文档的数组

使用Java在MongoDB中的嵌入式文档中索引和搜索“数组”

如何使用Mongoose将值推入MongoDB中嵌入式文档中的数组?

如何使用C#仅从多层嵌入式MongoDB文档中获取具有相应父元素的确切子元素

使用Spring Data MongoDB指定分片集合

使用spring-data-mongodb进行审计

在Spring Data Repositories中的列表中查找包含字符串的实体

Spring Data Rest - 嵌入式实体未在 POST 上反序列化

仅使用 Spring Data JDBC 中的值列表的实体的问题

如何使用mongoDB和spring数据在嵌入式文档中创建id

使用python(pymongo)在mongodb中编辑嵌入式文档

如何使用Spring Data MongoDB查询MongoDB文档中的JSON数组