Ludovic
Ludovic

Reputation: 263

MongoDB: Update nested field from another nested field with dot notation using MongoShell

I want to update an empty nested field from another nested field value. I'm using this Mongo Shell command:

db.myCollection.updateMany({"object2.field": ""}, { $set: {"object2.field": '$object1.field' } } );

But $object1.field is not resolved and the destination field actually contains the variable name instead of its content.

I've performed many tests and documentation searches but with no success.

Upvotes: 1

Views: 1005

Answers (1)

Ludovic
Ludovic

Reputation: 263

As turivishal said in his comment: Mongo Shell can not access another field for internal operation. Must use update with aggregation pipeline. In this case, just need to wrap update part in array bracket [{ $set: { "object1.field": "$object2.field" } }].

Upvotes: 2

Related Questions