Ram*_*ona 5 java spring mongodb aggregation-framework aws-documentdb
我正在使用 Spring 中的 DocumentDb,我想使用另一个字段的值对一个字段进行 updateMany。
以下查询在 MongoDb 控制台中运行:
db.mtpProject.update({}, [{$set: {"fieldToBeUpdated": "$myCurrentField"}}]).
Run Code Online (Sandbox Code Playgroud)
如果我删除 $set 之前的 [],则该字段不会使用 myCurrentField 的值更新,而是使用字符串“myCurrentField”更新。
在Java中我尝试像这样执行它:
MongoDatabase db = mongoTemplate.getDb();
MongoCollection collection = db.getCollection("myProject");
collection.updateMany(new Document(), new Document("$set", new
Document("fieldToBeUpdated", "$myCurrentField")));
Run Code Online (Sandbox Code Playgroud)
但因为是在没有数组的情况下创建它,所以只需将 fieldToBeUpdated 的值设置为“$myCurrentField”。
我提到我也尝试过将 $addFields 与 $out 一起使用,但 DocumentDb 不支持 $out。
Aggregation agg =
Aggregation.newAggregation(
aoc ->
new Document(
"$addFields",
new Document("fieldToBeUpdated", "$myCurrentField")),
aoc -> new Document("$out", "myProject"));
mongoTemplate.aggregate(agg, "myProject", MyProject.class);
Run Code Online (Sandbox Code Playgroud)
我希望使用保存在 myCurrentField 上的值更新字段,而不是使用字段名称。
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |