gpt4 book ai didi

python - 无法使用 PyMongo 进行日期查询

转载 作者:可可西里 更新时间:2023-11-01 10:51:33 30 4
gpt4 key购买 nike

问题如下。我有一个带有变量 jobdate 的 mongo 集合,我认为它现在是一个字符串。但是,我需要运行一个路由来按以下方式查询所述变量:

@app.route('/active_jobs/<jobdate>', methods = ['GET'])
def get_a_date(jobdate):

ajobs = mongo.db.ajobs

output = []

for q in ajobs.find({'jobdate':jobdate}):

output.append ({
'jobdate' : q['jobdate'], 'jobtime' : q['jobtime'],'plant': q['plant'],
'po': q['po'], 'company': q['company'], 'client': q['client'],
'jobaddress': q['jobaddress'], 'm3': q['m3'], 'use': q['use'],
'formula': q['formula'], 'placement': q['placement'],
'badmix1': q['badmix1'], 'badmix2': q['badmix2'], 'badmix3': q['badmix3'],
'confirmation': q['confirmation'],'status': q['status']
})

return jsonify({'result' : output})

这里的问题是,当我在 Postman 上尝试 GET 请求时,我只是得到一个空的 {'result' : } json 对象。我怀疑查询结构本身可能不是问题,而是日期格式。

我的 POST 请求如下,我如何格式化日期变量以使其可以查询。

@app.route('/active_jobs/new', methods=['POST'])
def add_job():

ajobs = mongo.db.ajobs

jobdate = request.json['jobdate']# date of job
jobtime = request.json['jobtime']# time of job
plant = request.json['plant']# plant for job
po = request.json['po']# production order
company = request.json['company']# client company name
client = request.json['client']# person in charge
jobaddress = request.json['jobaddress']#job address
use = request.json['use']# concrete use in site
m3 = request.json['m3']#job volume
formula = request.json['formula']#job formula
placement = request.json['placement']#type of placement
badmix1 = request.json['badmix1']#batch admixture add-on
badmix2 = request.json['badmix2']#batch admixture add-on
badmix3 = request.json['badmix3']#batch admixture add-on
confirmation = request.json['confirmation']#level of confirmation for job
status = request.json['status']#job status

ajob_id = ajobs.insert({
'jobdate' : jobdate, 'jobtime' : jobtime, 'plant': plant,
'po' : po, 'company' : company, 'client' : client, 'jobaddress' : jobaddress,
'use' : use, 'm3' : m3, 'formula' : formula, 'placement' : placement,
'badmix1' : badmix1, 'badmix2' : badmix2, 'badmix3' : badmix3,
'confirmation' : confirmation, 'status' : status
})

new_job = ajobs.find_one({'_id' : ajob_id})

output = ({
'jobdate' : new_job['jobdate'], 'jobtime' : new_job['jobtime'],'plant': new_job['plant'],
'po': new_job['po'], 'company': new_job['company'], 'client': new_job['client'],
'jobaddress': new_job['jobaddress'], 'm3': new_job['m3'], 'use': new_job['use'],
'formula': new_job['formula'], 'placement': new_job['placement'],
'badmix1': new_job['badmix1'], 'badmix2': new_job['badmix2'], 'badmix3': new_job['badmix3'],
'confirmation': new_job['confirmation'],'status': new_job['status']
})

return jsonify({'verify new job': output})

注意:对于应用程序而言,日期结构必须为以下 YYYY-MM-DD

最佳答案

虽然字符串匹配应该也可以,但是最好将日期作为日期对象存储在mongo中。您可以使用 strptime 来完成此操作。对于 YYYY-MM-DD 格式,它应该在 POST 中得到类似这样的结果:

from datetime.datetime import strptime

jobdate = strptime(request.json['jobdate'], "%Y-%m-%d").date()

关于python - 无法使用 PyMongo 进行日期查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39402679/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com