網路上查到的方法是使用
.aggregate([{ $sample: { size: 1 } }])
但是我實際上使用時它卻說是無效的語法
想請教各位原因出在哪裡,感謝><
import pymongo
class GetProxy():
def __init__(self):
self.DB_URI = pymongo.MongoClient(host='localhost', port=27017)
self.DB_NAME = self.DB_URI.ip_pool # 資料庫
self.DB_COLLECTION = self.DB_NAME.proxy # 資料表
def get_random_proxy(self):
result = self.DB_COLLECTION.aggregate([{ $sample: { size: 1 } }])
error code:
Traceback (most recent call last):
File "D:/【程式設計】/HSIAO_plan/get_proxy/get_proxy/get_proxy.py", line 1, in <module>
from runProxy import GetProxy
File "D:\【程式設計】\HSIAO_plan\get_proxy\get_proxy\runProxy.py", line 43
result = self.DB_COLLECTION.aggregate([{ $sample: { size: 1 } }])
^
SyntaxError: invalid syntax