[英]how to run mongo db query via python script?
我有兩個 mongo db 查詢
首先是:
db.sale_order.find().forEach(function(doc){doc.orderDate = new
Date(doc.orderDate);db.sale_order.save(doc);})
其次是:
db.sale_order.aggregate({$group: { _id: {year : { $year : "$orderDate" }, month : {$month : "$orderDate"},day :{ $dayOfMonth : "$orderDate"},},price : {$sum: "$price"}}} )
第二個查詢僅在運行第一個查詢后才有效,然后使用 python 執行其他操作。
所以運行這兩個查詢,我使用以下代碼:
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
然后它給出以下錯誤:
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
您可以使用 pymongo 的db.eval
函數。
例如您的查詢:
db.getCollection('test').update({filter},{update})
使用 pymongo 你可以這樣做:
db.eval("db.getCollection('test').update({filter},{update})")
在collection.aggregate
調用中指定pipeline
和options
時需要使用引號:
collection.aggregate({'$group': { '_id': {'year' : { '$year' : "$orderDate" }, 'month' : {'$month' : "$orderDate"},'day' :{ '$dayOfMonth' : "$orderDate"},},'price' : {'$sum': "$price"}}})
此外,您不能在 python 中直接使用 mongodb Date
構造函數。 您可以改為使用bson.Code
在 Python 中傳遞 javascript 代碼。
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);}"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.