2016-03-15 111 views
0

我努力理解什麼是錯的語法如下:蟒越獄美元符號

from pymongo import MongoClient 

client = MongoClient('localhost', 27017) 
db = client.monitor 
#cursor = db.monitoring_logs.find({"widget": "56dfed49a2988d9019000585;"}) 
cursor = db.monitoring_logs.find({"widget": {$in:["56dfed49a2988d9019000585;","56d58f5b1dc95f54460002f6;"]}}) 
print (cursor.count()) 

錯誤消息:

C:\Users\Nir.Regev\Anaconda3\python.exe C:/Users/Nir.Regev/PycharmProjects/anomaly/get_mongo_data.py 
    File "C:/Users/Nir.Regev/PycharmProjects/anomaly/get_mongo_data.py", line 6 
    cursor = db.monitoring_logs.find({"widget": {$in:["56dfed49a2988d9019000585;","56d58f5b1dc95f54460002f6;"]}}) 
               ^
SyntaxError: invalid syntax 

Process finished with exit code 1 

是美元符號($),如果是如何得到解決這個 ?

+1

嘗試使用'「$ in」'代替。 – Rames

回答

3

你需要把引號的任何特殊字符串在Python(「$在」在這種情況下) - 你可能會尋找在其使用JavaScript編寫的Mongo的文檔,而不是pymongo客戶端的文檔,這是寫對於Python客戶端(我也這樣做了;))。

from pymongo import MongoClient 

client = MongoClient('localhost', 27017) 
db = client.monitor 
#cursor = db.monitoring_logs.find({"widget": "56dfed49a2988d9019000585;"}) 
cursor = db.monitoring_logs.find({"widget": {'$in': ["56dfed49a2988d9019000585;","56d58f5b1dc95f54460002f6;"]}}) 
print (cursor.count())