django - how to run mongo db query via python script? -


i have 2 mongo db query

first is:

 db.sale_order.find().foreach(function(doc){doc.orderdate = new          date(doc.orderdate);db.sale_order.save(doc);}) 

and second is:

db.sale_order.aggregate({$group: { _id: {year : { $year : "$orderdate" }, month : {$month : "$orderdate"},day :{ $dayofmonth : "$orderdate"},},price : {$sum: "$price"}}} ) 

this second query work after running first query , after performing other operation using python .

so running 2 query , using following code :

def create_demo_accounts():     account.objects.all().delete()     client = mongoclient('localhost', 27017)     db = client['mydb']     collection = db['sale_order']      #collection.find().foreach(function(doc){doc.orderdate = new date(doc.orderdate);db.sale_order.save(doc);})     doc in collection.find():       doc['orderdate'] = date(doc['orderdate'])  # line 127       db.sale_order.save(doc)      try:         post in collection.aggregate({$group: { _id: {year : { $year : "$orderdate" }, month : {$month : "$orderdate"},day :{ $dayofmonth : "$orderdate"},},price : {$sum: "$price"}}} ):   # line 130           oid = post['_id']           try:             year = post['year']             month = post['month']             day = post['dayofmonth']             price = post['price']             account.objects.create(days = day, sales=price,                                  expenses=400, ceo="welch")           except:             pass     except:       pass 

then giving following error :

syntaxerror @ / invalid syntax (utils.py, line 130) request method: request url:    http://127.0.0.1:8000/ django version: 1.8.4 exception type: syntaxerror exception value:     invalid syntax (utils.py, line 130) exception location: /home/shubham/music/progress/django-graphos-master/demo_project/demo/views.py in <module>, line 12 python executable:  /usr/bin/python python version: 2.7.6 python path:     ['/home/shubham/music/progress/django-graphos-master/demo_project',  '/usr/local/lib/python2.7/dist-packages/mango-0.1-py2.7.egg',  '/usr/local/lib/python2.7/dist-packages/django_mongo_auth-0.1.2-py2.7.egg',  '/usr/local/lib/python2.7/dist-packages/django_mongoengine-0.1.1-py2.7.egg',  '/usr/local/lib/python2.7/dist-packages/django_graphos-0.0.2a0-py2.7.egg',  '/usr/lib/python2.7',  '/usr/lib/python2.7/plat-x86_64-linux-gnu',  '/usr/lib/python2.7/lib-tk',  '/usr/lib/python2.7/lib-old',  '/usr/lib/python2.7/lib-dynload',  '/usr/local/lib/python2.7/dist-packages',  '/usr/lib/python2.7/dist-packages',  '/usr/lib/python2.7/dist-packages/pilcompat',  '/usr/lib/python2.7/dist-packages/gtk-2.0',  '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',  '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode'] server time:    mon, 21 sep 2015 12:26:15 +0530 

you can use pymongo's db.eval function.
e.g. query:

db.getcollection('test').update({filter},{update}) 

using pymongo can like:

db.eval("db.getcollection('test').update({filter},{update})") 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -