Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Python将Javascript new Date()插入MongoDB?_Javascript_Python_Mongodb - Fatal编程技术网

如何通过Python将Javascript new Date()插入MongoDB?

如何通过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

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

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
对象有什么问题?