如何在 spring-boot 中使用 MongoTemplate 使用另一个字段更新 MongoDB 集合字段

Sam*_*hna 5 java mongodb spring-boot mongotemplate

我需要使用 spring-boot 中的 mongo 模板使用另一个字段来更新字段值。我知道如何使用 MongoDB 查询来完成此操作。但无法找到一种MongoTemplate从数据库级别使用而不将数据加载到内存的方法。

我正在寻找这样的东西。位于value1集合中,需要根据嵌套数组 ( menu) 中的值进行更新,并且第 0 个索引始终包含该值。

    Query query = new Query();
    query.addCriteria(Criteria.where("price").is(0));

    Update update = new Update();
    update.set("price", "$menu[0].sellingPrice");

    mongoTemplate.updateMulti(query, update, CollectionName);
Run Code Online (Sandbox Code Playgroud)