Reputation: 5127
First of all, I am not a mongo db expert, so I might have some newbie mistakes. I have a mongodb database that was created with pymongo a while ago, from parsing a json. A sample piece of code is this:
data = {parsed_json['location']['city']:
'local_time': parsed_json['current_observation']['local_time_rfc822'],
'full_name': parsed_json['current_observation']
['observation_location']['full'],
'latitude': float(parsed_json['current_observation']
['observation_location']
['latitude']),
'longitude': float(parsed_json['current_observation']
['observation_location']
['longitude']),
'elevation_ft': float(parsed_json['current_observation']
['observation_location']
['elevation'].split(' ')[0])
}}
collection = db[state_to_insert]
collection.insert_one(data)
print 'Inserted data successfully'
The output in the db looks like the one below, however, one of the fields "local_time" is saved in the wrong format (string instead of date).
{
"_id" : ObjectId("5adb54ff59aea50aec2856bf"),
"Ithaca" : {
"weather" : "Clear",
"full_name" : "Ithaca, New York",
"windchill_f" : 47.0,
"solarradiation" : 786,
"heat_index" : "NA",
"latitude" : 44.7,
"wind_mph" : 0.0,
"dewpoint" : 23,
"precip_today_in" : 0.0,
"temp_f" : 47.0,
"elevation_ft" : 174.0,
"pressure_trend" : "0",
"visibility_mi" : 10.0,
"wind_string" : "Calm",
"pressure_in" : 30.34,
"wind_dir" : "NNW",
"wind_degrees" : 336.0,
"relative_humidity_perc" : 0.38,
"uv" : 3.7,
"longitude" : -73.47,
"local_time" : "Sat, 21 Apr 2018 11:13:03 -0400",
"wind_gust_mph" : 0.0,
"feels_like" : 47
}
}
My db is running on a local machine, in a db that has one collection called "NY":
To fix the data type, so I can query the database properly by date, in robomongo, I run the following code:
db.NY.find().forEach(function(element){
element.local_time = new Date(element.local_time);
db.NY.save(element);
})
Which produces the following output:
{
"_id" : ObjectId("5adb54ff59aea50aec2856bf"),
"Ithaca" : {
"weather" : "Clear",
"full_name" : "Ithaca, New York",
"windchill_f" : 47.0,
"solarradiation" : 786,
"heat_index" : "NA",
"latitude" : 44.7,
"wind_mph" : 0.0,
"dewpoint" : 23,
"precip_today_in" : 0.0,
"temp_f" : 47.0,
"elevation_ft" : 174.0,
"pressure_trend" : "0",
"visibility_mi" : 10.0,
"wind_string" : "Calm",
"pressure_in" : 30.34,
"wind_dir" : "NNW",
"wind_degrees" : 336.0,
"relative_humidity_perc" : 0.38,
"uv" : 3.7,
"longitude" : -73.47,
"local_time" : "Sat, 21 Apr 2018 11:13:03 -0400",
"wind_gust_mph" : 0.0,
"feels_like" : 47
},
"local_time" : ISODate("1970-01-01T00:00:00.000Z")
}
So, the function seems to work in robomongo, however, the behavior is not the expected one since:
"local_time" : "Sat, 21 Apr 2018 11:13:03 -0400"
Is transformed into:
"local_time" : ISODate("1970-01-01T00:00:00.000Z")
Furthermore, all the other dates in the record are the same. What's the problem here? Anybody can point out where the error is?
Upvotes: 0
Views: 1001
Reputation: 37018
There is no element.local_time
in your forEach
. element
here is the document, so it should be:
db.NY.find().forEach(function(element){
element.local_time = new Date(element.Ithaca.local_time);
db.NY.save(element);
})
Upvotes: 1