I wanted to know if there is an easy way to set a field using the existing value as the key of hash map. The hashmap is declared before the query. e. g.
I’ve tried to do this and the result is null. I am guessing the current value (“$field”) isn’t evaluated as a string. Is there a better way of doing what I am trying to do?
Can you provide concrete examples of expected behaviour in the form of before and after sample documents?
I am not too sure about the use case.
The code above does not work because the expression hashMap[“$field”] is evaluated before db.example.update is being called. So the server receive a single value, most likely undefined (in JS).
There was a mistake in the db where a field from one collection was mapped to the wrong field in another collection. The expected behavior was to find the records in the affected collection and replace the field that was incorrectly mapped to the correct field in the other collection.
Collection A has field1 and field 2. Collection B has documents where field3 (in Collection A) was supposed to be mapped to field2 but was instead mapped to field1.
The list of distinct values in Collection A was relatively small so I was thinking I could create a map where the key would be the value of field1 and the value would be field2. I was hoping I could use the value of field3 (Collection B) as the key of the array and set field3 to the value.
Thank you for explaining why that will not work. Another dev that I work with helped me out. She built an aggregation pipeline that does a lookup on Collection A using field3 as the local value and field1 as the foreign key and uses that result to project field2 from Collection A onto Collection B.