2

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.

2
  • 3
    it can not access another field for internal operation, use update with aggregation pipeline, just need to wrap update part in array bracket [{ $set: { "object1.field": "$object2.field" } }] Commented Feb 23, 2021 at 16:31
  • 1
    It just works. So frustrating to have spent that amount of time for a pair of brackets. Thank you :) Commented Feb 23, 2021 at 16:36

1 Answer 1

3

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" } }].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.