如何在 Python 的 MongoDB ISODate 字段中搜索?
How can I search in MongoDB ISODate field from Python?
我收到了一个 API 的查询。这个 API 在 Python.
我从 django 应用程序调用它 (views.py)。然后,我想查询我的 MongoDB 集合,使用 mongoengine:
api_response = requests.get("http://*******", {'query':query}) #We call the API
json_resp = api_response.json()
person = Person.objects(__raw__=json_resp).to_json() #We search for the json_query in the DB (raw) and the output is JSON
它工作正常,但我的日期有问题...确实,我的 Person 模型如下:
class Person(DynamicDocument):
# Meta Variables
meta = {
'collection':'personsExample'
}
#Document variables
PersonID = models.CharField(max_length = 6)
FirstName = models.CharField(max_length = 50)
LastName = models.CharField(max_length = 50)
Gender = models.CharField(max_length = 6) #male/female
BirthDate = models.DateField()
CitizenCountryCode = models.CharField(max_length = 2)
我的 personsExample 集合是通过 mongoimport 从 CSV 文件导入的:
mongoimport --host localhost --db persons --collection personsExample --type csv --file reducedPersonsExtract.csv --headerline
由于出生日期设置为字符串,我使用以下方法对其进行了转换:
db.personsExample.find().forEach(function(el){el.BirthDate = new ISODate(el.BirthDate); db.personsExample.save(el)})
我现在遇到的问题是它给 BirthDate 字段如下:
"BirthDate" : ISODate("1970-12-21T00:00:00Z")
但在我的 json 查询中,日期存储为
datetime.datetime(1970,12,21,0,0,0).isoformat()
给出:
{
"BirthDate": "1970-12-21T00:00:00"
}
因此,查询不起作用,我需要使用
进行查询
{
"BirthDate": ISODate"1970-12-21T00:00:00Z"
}
(但我无法使用 Python... 创建此类对象 (ISODate))
或者寻找另一种方法将日期存储在 MongoDB.
中
请问您是否知道如何解决我的问题?
我已经成功地完成了我想做的事情。事实上,我没有在数据库中将日期转换为 ISODate,我将它们存储为字符串 "YYYY-MM-DD"。然后,我在我的 API 中格式化日期(它将 JSON 查询发送到使用 MongoDB 的应用程序):
my_dict['BirthDate'] = datetime.datetime(YYYY,MM,DD).isoformat()
然后,在我的应用程序中:
json_resp = api_response.json() #Deserialization of the response
json_resp['BirthDate'] = datetime.datetime.strptime(json_resp['BirthDate'], "%Y-%m-%dT%H:%M:%S")
这可能不是最好的解决方案,但它确实有效。
我收到了一个 API 的查询。这个 API 在 Python.
我从 django 应用程序调用它 (views.py)。然后,我想查询我的 MongoDB 集合,使用 mongoengine:
api_response = requests.get("http://*******", {'query':query}) #We call the API
json_resp = api_response.json()
person = Person.objects(__raw__=json_resp).to_json() #We search for the json_query in the DB (raw) and the output is JSON
它工作正常,但我的日期有问题...确实,我的 Person 模型如下:
class Person(DynamicDocument):
# Meta Variables
meta = {
'collection':'personsExample'
}
#Document variables
PersonID = models.CharField(max_length = 6)
FirstName = models.CharField(max_length = 50)
LastName = models.CharField(max_length = 50)
Gender = models.CharField(max_length = 6) #male/female
BirthDate = models.DateField()
CitizenCountryCode = models.CharField(max_length = 2)
我的 personsExample 集合是通过 mongoimport 从 CSV 文件导入的:
mongoimport --host localhost --db persons --collection personsExample --type csv --file reducedPersonsExtract.csv --headerline
由于出生日期设置为字符串,我使用以下方法对其进行了转换:
db.personsExample.find().forEach(function(el){el.BirthDate = new ISODate(el.BirthDate); db.personsExample.save(el)})
我现在遇到的问题是它给 BirthDate 字段如下:
"BirthDate" : ISODate("1970-12-21T00:00:00Z")
但在我的 json 查询中,日期存储为
datetime.datetime(1970,12,21,0,0,0).isoformat()
给出:
{
"BirthDate": "1970-12-21T00:00:00"
}
因此,查询不起作用,我需要使用
进行查询{
"BirthDate": ISODate"1970-12-21T00:00:00Z"
}
(但我无法使用 Python... 创建此类对象 (ISODate)) 或者寻找另一种方法将日期存储在 MongoDB.
中请问您是否知道如何解决我的问题?
我已经成功地完成了我想做的事情。事实上,我没有在数据库中将日期转换为 ISODate,我将它们存储为字符串 "YYYY-MM-DD"。然后,我在我的 API 中格式化日期(它将 JSON 查询发送到使用 MongoDB 的应用程序):
my_dict['BirthDate'] = datetime.datetime(YYYY,MM,DD).isoformat()
然后,在我的应用程序中:
json_resp = api_response.json() #Deserialization of the response
json_resp['BirthDate'] = datetime.datetime.strptime(json_resp['BirthDate'], "%Y-%m-%dT%H:%M:%S")
这可能不是最好的解决方案,但它确实有效。