java - 如何更新 MongoDB 中的嵌套文档而不是使用 Spring Data Java

标签 java spring mongodb spring-data spring-data-mongodb

我正在尝试更新嵌套 mongo 文档中的所有空“角色”属性,如下所示:

 {
            "id": "5a46ae611331fb6b8254a271",
            "name": "my_Customer",
            "users": [
                {
                    "lastName": "ZoozFirstNameUser",
                    "firstName": "ZoozLastNameUser",
                    "id": "5a46ae611331fb6b8254a26e",
                    "role": null
                },
                {
                    "lastName": "Vitaly",
                    "firstName": "Treck",
                    "id": "5a46ae611331fb6b8254a26f",
                    "role": null
                }
            ]
    }

我尝试使用以下命令来执行此操作,但它不起作用:

     @Override
          public int updateUserRole(Role newRole) {

                  Query query = Query.query(Criteria.where("users")
                            .elemMatch(Criteria.where("role").is(null)));

                  Update update = new Update().set("users",                      
                           new BasicDBObject("role", newRole));

             return mongoTemplate.updateMulti(query, update, Customer.class).getN();
         }

我没有收到任何错误,但执行此命令后没有任何更改...:(

请帮忙

最佳答案

尝试这个更新语句

Update update = new Update().set("users.$.role", newRole);

关于java - 如何更新 MongoDB 中的嵌套文档而不是使用 Spring Data Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48028607/

相关文章:

java - 无法获取 Windows ProductId key ,但可以获取 ProductName key

java - 从 Spring 3.0 开始用 SpEL 替换 JSP 中的 EL

mongodb - 查询 Mongodb 的文档,这些文档的子对象在关联数组中具有特定的 elem 值

spring - 使用 Spring Data JPA 进行延迟获取

java - spring bean实现接口(interface)的bean ID是多少

node.js - res.send() 不发送当前响应,而是保留最后一个响应

蒙戈数据库 4.2.8 : Unable to add session into the cache because the number of active sessions is too high

java - 如何在 java 中读取/写入异或 txt 文件 UTF8?

java - Carrot2 在搜索时不显示所有包含特定单词的簇

java - Angular 6 访问 REST 失败,Access-Control-Allow-Origin