Can't make a date query with PyMongo
<p>the issue is the following. I have a mongo collection with the variable jobdate, which is as of now a string I would suppose.However, I need to run a route to query said variable in the following way:</p>

<pre><code>@app.route('/active_jobs/&lt;jobdate&gt;', 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})
</code></pre>

<p>the issue here is the fact that when I try a GET request on Postman, I simply get an empty {'result' : } json object. I suspect the query structure itself may not be the problem but the date formatting.</p>

<p>my POST request is as follows, how could I format the date variable to make it queryble so-to-speak.</p>

<pre><code>@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})
</code></pre>

<p>NOTE: for purposes of the app, the date structure must be the following YYYY-MM-DD </p>

<p>Although string matching should also work, it would be best to store the date in mongo as a date object. You can do so by using strptime. For the format YYYY-MM-DD it should work out to something like this in POST:</p>

<pre><code>from datetime.datetime import strptime

jobdate = strptime(request.json['jobdate'], "%Y-%m-%d").date()
</code></pre>

python mongodb date pymongo