I'm using pymongo (python 3.6 with mlab), and I want to update all the documents in my collection. in my documents, I have data stored like this :
{
"_id": {
"$oid": "5981a77e1d41c81419b60414"
},
"Ticker": "GOOGL",
"Sector": "USTECH",
"Market": "NASDAQ",
"Data": [
{
"Date": "Jul 27, 2017",
"Price": "966.41",
"Open": "969.52",
"High": "969.52",
"Low": "963.50",
"Vol": "743.92K",
"Change%": "0.11"
},
{
"Date": "Jul 26, 2017",
"Price": "965.31",
"Open": "972.78",
"High": "973.95",
"Low": "960.23",
"Vol": "2.22M",
"Change%": "-0.38"
}]}
And I want to update all the dates so they look like this : 1997-1-6 instead of Jan 06, 1997. I have this code :
connection = MongoClient(link)
dbase = connection[db_name]
collection = dbase.historicals
client = collection.find()
if client:
collection.update({}, "$set" : {"Data.Date":datetime.strptime('Date', '%b %d, %Y').date()})
connection.close()
I have tried update_many, but it seems the whole update syntax is not correct, like if it doesn't get acess to the Date field. Any tips ?
Edit : when I try this code :
client = collection.update_many({}, {"$set": {"Data": {"Date": '2000'}}})
it works, but this one doesn't client = collection.update_many({}, {"$set": {"Data": {"Date": datetime.strptime("Date", '%b %d, %Y').date()}}})
尝试从更新查询中删除“数据”,并在“$ set”之前和之后添加{}
collection.update({}, {"$set" : {"Date":datetime.strptime('Date', '%b %d, %Y').date()}})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.