DocumentDb- spring- update field with other field value

Viewed 241

I am using DocumentDb from Spring and I want to make an updateMany on a field with the value of another field.

The following query is working in MongoDb Console:

 db.mtpProject.update({}, [{$set: {"fieldToBeUpdated": "$myCurrentField"}}]).

If I remove the [] before the $set, the field is not updated with the value of myCurrentField but with the String "myCurrentField".

in Java I try to execute it like this:

  MongoDatabase db = mongoTemplate.getDb();
  MongoCollection collection = db.getCollection("myProject");
  collection.updateMany(new Document(), new Document("$set", new 
  Document("fieldToBeUpdated", "$myCurrentField")));

But because is creating it without an array, is just setting the value of fieldToBeUpdated to "$myCurrentField".

I mention that I tried also with $addFields with $out, but $out is not supported by DocumentDb.

 Aggregation agg =
                Aggregation.newAggregation(
                        aoc ->
                                new Document(
                                        "$addFields",
                                        new Document("fieldToBeUpdated", "$myCurrentField")),
                        aoc -> new Document("$out", "myProject"));

        mongoTemplate.aggregate(agg, "myProject", MyProject.class);

I would expect to update the fields with the values saved on myCurrentField, and not with the field name.

0 Answers
Related