'How to search and replace if you need to search for multiple keys (pymongo)?
I am successfully producing different processes with a test base (mongodb), until I met the next moment. I need to update/replace documents in the database based on two keys (maybe more). How can this be implemented?
These processes are successfully executed with one key in the given code:
def replace_with_csv(self, data, key_1):
df = pd.read_csv(data, low_memory=False)
df = df.to_dict('records')
try:
startTime = time.time()
for row in df:
self.collection.find_one_and_replace({key_1: row[key_1]}, row)
endTime = time.time()
totalTime = endTime - startTime
totalTime = str('{:>.3f}'.format(totalTime))
But what if there are two or more? It seems logical to pass them as a list (keys_list = [key_1, key_2]). But how will it be correct to modify the string, if i pass a list of keys:
for row in df:
self.collection.find_one_and_replace({key_1: row[key_1]}, row)
Can I do these processes using an index configured on the collection itself? Since now it is complex and consists of two fields: date_1_region_code_1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

