如何通过Python将Javascript new Date()插入MongoDB?
Javascript如何通过Python将Javascript new Date()插入MongoDB?,javascript,python,mongodb,Javascript,Python,Mongodb,Javascript theTime = new Date(); $.getJSON("/my_path",{ the_time: theTime, format: 'json' }); var theTime = new Date(); var theTime = theTime.toISOString() $.getJSON("/my_path",{ the_time: theTime, format: 'json' }); var date = new
theTime = new Date();
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var theTime = new Date();
var theTime = theTime.toISOString()
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var date = new Date();
var isoDateString = date.toISOString();
$.getJSON("/save_date",{isoDateString: isoDateString, format: 'json'}, function(results) {
});
"date_object" : ISODate("2016-08-21T06:41:19.319Z"),
Python
var the_time = request.GET.the_time
# the entry
entry = {}
# other key/values are added to entry
entry['entry_time'] = entry_time
# the document
new_document = {}
# the entry is nested in the new document
new_document['k1']['n1'].append(entry)
collection.insert(new_document)
MongoDB
当我看到RockMongo中的条目时,它只是显示:
"entry_time": "Mon Jan 18 2016 23:59:51 GMT+1000 (AEST)"
但根据这个答案:
它应该是这样的:
ISODate("2014-02-10T10:50:57.240Z")
如何通过Python将Javascript new Date()作为ISODate
插入MongoDB
编辑:
我也试过这个:
Javascript
theTime = new Date();
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var theTime = new Date();
var theTime = theTime.toISOString()
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var date = new Date();
var isoDateString = date.toISOString();
$.getJSON("/save_date",{isoDateString: isoDateString, format: 'json'}, function(results) {
});
"date_object" : ISODate("2016-08-21T06:41:19.319Z"),
这就导致了RockMongo:
"entry_time": "2016-01-18T14:35:09.226Z"
JavaScript
theTime = new Date();
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var theTime = new Date();
var theTime = theTime.toISOString()
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var date = new Date();
var isoDateString = date.toISOString();
$.getJSON("/save_date",{isoDateString: isoDateString, format: 'json'}, function(results) {
});
"date_object" : ISODate("2016-08-21T06:41:19.319Z"),
在Python中解析UTC日期字符串并将日期对象保存到MongoDB中
Python
import datetime
isoDateString = request.GET.isoDateString
date_object = datetime.datetime.strptime(isoDateString, '%Y-%m-%dT%H:%M:%S.%fZ')
document = {}
document['date_object'] = date_object
# define database
dbname = 'my_database'
# define collection
collection = db.my_collection
# insert document
collection.insert(document);
结果
theTime = new Date();
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var theTime = new Date();
var theTime = theTime.toISOString()
$.getJSON("/my_path",{
the_time: theTime,
format: 'json'
});
var date = new Date();
var isoDateString = date.toISOString();
$.getJSON("/save_date",{isoDateString: isoDateString, format: 'json'}, function(results) {
});
"date_object" : ISODate("2016-08-21T06:41:19.319Z"),
希望这对您有所帮助。您不应该向Mongo传递字符串,而应该向其传递Python。应该能够解析字符串:
“Mon Jan 18 2016 23:59:51 GMT+1000(AEST)”
从dateutil.parser import*然后解析(“Mon Jan 18 2016 23:59:51 GMT+1000(AEST)”
返回datetime.datetime(2016,1,18,23,59,51,tzinfo=tzlocal())
。获取datetime
对象有什么问题?