2

I have two 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 is only work after running first query , after that performing other operation using python .

so running this two query , I am 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);})
    for doc in collection.find():
      doc['orderDate'] = Date(doc['orderDate'])  # line 127
      db.sale_order.save(doc) 
    try:
        for 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 it is giving following error :

SyntaxError at /
invalid syntax (utils.py, line 130)
Request Method: GET
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
0

2 Answers 2

1

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

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

using pymongo you can do like:

db.eval("db.getCollection('test').update({filter},{update})")
Sign up to request clarification or add additional context in comments.

1 Comment

0

You need to use quotes when specifying pipeline and options in collection.aggregate call:

collection.aggregate({'$group': { '_id': {'year' : { '$year' : "$orderDate" }, 'month' : {'$month' : "$orderDate"},'day' :{ '$dayOfMonth' : "$orderDate"},},'price' : {'$sum': "$price"}}})

Also, you can't directly use mongodb Date constructor in python. You can instead use bson.Code to pass the javascript code in Python .

import bson # import bson library
# pass the javascript code string in bson 'Code' object
collection.find().forEach(bson.Code("function(doc){doc.orderDate = new Date(doc.orderDate);db.sale_order.save(doc);}"))

2 Comments

ya its work, but again have error on line 127 global name 'Date' is not defined
You can use bson.Code to pass the javascript function to .forEach() in your script. updated the ans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.