mongotest.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from pymongo import MongoClient
  2. import pymongo
  3. import datetime
  4. #CONNECTION_STRING = "mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/test"
  5. CONNECTION_STRING = "mongodb://mongoAdmin:chan9eM3@hhh.ptt.cx"
  6. #CONNECTION_STRING = "mongodb://mongoAdmin:chan9eM3@hhh.ptt.cx/logs"
  7. client = MongoClient(CONNECTION_STRING)
  8. print(client)
  9. cols=client['logs']
  10. #print(cols)
  11. col=cols['syslog']
  12. #cursor=col.find({}).sort('time_rcvd',pymongo.DESCENDING)
  13. cursor=col.find({})
  14. #cursor=col.find({}).sort('time_rcvd',pymongo.DESCENDING)
  15. cnt=0
  16. for c in cursor:
  17. # print(c)
  18. # if 'clickbot_100' in c['msg']:
  19. if 'poibot' in c['msg']:
  20. dt=c['time_rcvd']
  21. dt=dt.replace(tzinfo=datetime.timezone.utc) #Convert it to an aware datetime object in UTC time.
  22. dt=dt.astimezone() #Convert it to your local timezone (still aware)
  23. print(dt.strftime('%Y-%m-%d %H:%M:%S'))
  24. # print(dt.strftime("%d %b %Y %I:%M:%S:%f %p"))
  25. # print(c['time_rcvd'])
  26. print(c['msg'])
  27. cnt+=1
  28. # if cnt>=100000:
  29. # break
  30. #print(col)
  31. #col.insert_one({"item_name" : "Bread",'id':1234,'name':'中文測試'})