Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2. import pymongo
  3.  
  4.  
  5. def get_collection():
  6. # 连接服务器
  7. client = pymongo.MongoClient('127.0.0.1', 27017)
  8.  
  9. # 连接数据库
  10. db = client.my_users
  11.  
  12. # 获取 collection
  13. user_collection = db.user_collection
  14.  
  15. return user_collection
  16.  
  17.  
  18. class User:
  19. def __init__(self, username, password):
  20. self.username = username
  21. self.password = password
  22.  
  23. def save(self):
  24. user_dict = {'username': self.username, 'password': self.password}
  25. rec_id = get_collection().insert(user_dict)
  26. print(rec_id)
  27.  
  28. @staticmethod
  29. def query_users():
  30. return get_collection().find()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement