5

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.

Ramona
  • 51
  • 2
  • $out is now supported on Amazon DocumentDB: https://aws.amazon.com/about-aws/whats-new/2020/09/amazon-documentdb-with-mongodb-compatibility-adds-aggregration-stage-increases-number-of-connections-and-cursors/ – Joseph Idziorek Sep 22 '20 at 18:41

0 Answers0