I have a dataframe example as follows:
request new_request
0 Whats the time What's the time?
1 its late it's late!
I then have a mongodb collection as follows:
{
'_id': 'b5445',
'request': 'Whats the time',
'accepted': True,
'aId': '6954c4',
'createDate': '1610726929114'
}
{
'_id': '4534fg', ,
'request': 'its late',
'accepted': True,
'aId': 'a86dfsb',
'createDate': '1610964941537'
}
I want to update the mongoDB collection so that it looks like:
{
'_id': 'b5445',
'request': "What's the time?",
'accepted': True,
'aId': '6954c4',
'createDate': '1610726929114'
}
{
'_id': '4534fg',
'request': "it's late!",
'accepted': True,
'aId': 'a86dfsb',
'createDate': '1610964941537'
}
I am new to mongoDB so completely stuck. How do i do that?
Here is an example:
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['mydatabase']
mycol = mydb['customers']
#pandas dataframe
df = pd.DataFrame(columns = ['request', 'new_request'],
data = {'request': ['Whats the time', '''its late'''],
'new_request': ['''What's the time?''', '''it's late!''']})
#Update MongoDB collection one by one
for i in range(len(df)):
myquery = { 'request': df.iloc[i]['request'] }
newvalues = { '$set': { 'request': df.iloc[i]['new_request'] } }
mycol.update_one(myquery, newvalues)
More details here: https://www.w3schools.com/python/python_mongodb_update.asp