【问题标题】:Is it possible to pull an array of strings from an Array whithin the document in Spring Boots MongoTemplate?是否可以从 Spring Boot MongoTemplate 文档中的数组中提取字符串数组?
【发布时间】:2022-01-22 22:34:19
【问题描述】:

我有一个字符串数组:

List<String> namesArray = new ArrayList<>()

然后我的 MongoDB 中有以下两个文档:

{
    "_id": {
        "$oid": "9873hihy9032u49083b98"
    },
    "type": "A",
    "config": {
        "names": ["Marc", "Franc"],
    }
}

{
    "_id": {
        "$oid": "803bkjiug34909832u40"
    },
    "type": "A",
    "config": {
        "names": ["Max", "Brian", "Marc"],
    }
}

我想从每个文档的config.names 数组中删除出现在namesArray 中的所有名称。

例如,如果名称 Marc 包含在数组中,则应将其从两个文档中删除,等等。

我试过这个没有任何运气:

Query query = Query.query(Criteria.where("type").is("A").and("config.names").in(namesArray));
Update update = new Update().pull("config.names", query)
mongo.updateMulti(new Query(), update, Person.class);

【问题讨论】:

    标签: spring mongodb spring-boot criteria mongotemplate


    【解决方案1】:

    你需要使用$elemMatch

    见:

    mongodb documentation

    spring data documentation

    【讨论】:

    • 我实际上看过$elemMatch,但我无法将它包含在查询中。无论我把它放在哪里,它都会给我一个错误。
    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多