0

I wanted to update a number of records in my mongodb collection, so I wanted to duplicate the field value to another field. Here is my code:

res = result_collection.update_many(filter = {'requestid' : { '$in': str_req_ids }},
                                             update = {'$set': { 'original_requestid': '$requestid' }},
                                             session = session 

I was expecting that the field original_requestid will be the value of the field of requestid, but it seems the value saved is literally $requestid instead. How to make sure that $requestid here was parsed as a value from the collection instead of a literal string?

I saw there is some approach using this link, but I prefer to use the update_many functionality rather than eval code which may cause some issues later.

rcs
  • 6,713
  • 12
  • 53
  • 75
  • If you `update_many` using an [aggregation pipeline](https://www.mongodb.com/docs/manual/tutorial/update-documents-with-aggregation-pipeline/), it should work for you. – rickhg12hs Jun 22 '23 at 12:10
  • nevermind, I end up using `$rename` instead. – rcs Jun 22 '23 at 13:51

0 Answers0