python - Best practice for using datetime in Pymongo query path? -
how can use datetime object in case need complex path? example want increment counter located in jumps/datetime.date()/country code
:
database['data'].update_one( {'some_data' : 'asdasd'}, {'$inc' : {'jumps.{}.{}'.format(datetime.now().date(), "us") : 1}}, )
this code work, (as expected) there string instead of datetime object:
"jumps" : { "2017-04-14" : { "us" : 4 } }
it's impossible store date object key in mongodb document. also, shouldn't use value key, because can't use keys in queries or indexes. better if change structure of document , simplify it.
something that:
"jumps" : {[ {'date': isodate('2017-04-14'), "us" : 4}, {'date': isodate('2017-04-14'), "ru" : 9} ]}
or can create new collection jumps
contain documents:
{'date': isodate('2017-04-14'), "us" : 4} {'date': isodate('2017-04-14'), "ru" : 9}
or structure
look @ article https://derickrethans.nl/mongodb-arbitrary-key-names.html
Comments
Post a Comment