'i don't get mongodb collection shard key with pymongo
i need to copy db meta to anthoer , i have done copy index, but i couldn't get the shard key of colleciont , so what should I do ? can you give me a example ?
cnn = pymongo.MongoClient(mongo_uri)
dbadmin = cnn.admin
dbadmin.command('enableSharding',new_db_nm)
dbadmin.command({'shardCollection': coll1, 'key': {'key1': 'hashed'}})
i want get the key1 from old_db
Solution 1:[1]
You can do that by specifying the date format in pandas. Remember pandas by default will show dates as year-month-date.
df.index=pd.to_datetime(df.index,format='%d/%m/%Y')
#output:
value
2020-03-18 0
2020-03-19 3
2020-03-20 5
2020-03-21 1
2020-03-22 6
2020-03-23 3
2022-01-11 558
2022-01-12 0
If you want to verify that's reading correctly the month and the day you can create columns with day and month number. Notice it's reading the last row as January 12
df.index=pd.to_datetime(df.index,format='%d/%m/%Y')
df['dates_str']=df.index.map(lambda x: x.strftime('%b %d %Y'))
df['day']=df.index.day
df['month']=df.index.month
df['year']=df.index.year
#output
value dates_str day month year
2020-03-18 0 Mar 18 2020 18 3 2020
2020-03-19 3 Mar 19 2020 19 3 2020
2020-03-20 5 Mar 20 2020 20 3 2020
2020-03-21 1 Mar 21 2020 21 3 2020
2020-03-22 6 Mar 22 2020 22 3 2020
2020-03-23 3 Mar 23 2020 23 3 2020
2022-01-11 558 Jan 11 2022 11 1 2022
2022-01-12 0 Jan 12 2022 12 1 2022
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
