-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.py
47 lines (40 loc) · 1.05 KB
/
mongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 操作mongo '
__author__ = 'zzyfisher'
import json
from pymongo import MongoClient
import time
from xitekInfo import ThreadInfo,PostInfo,PageInfo,ForumInfo
class MongoStore:
#构造
def __init__(self):
self.conn=b""
self.db=b""
#打开db
def open(self):
self.conn = MongoClient("127.0.0.1",27017)
self.db = self.conn.xitek
#保存到db-thread(主题)
def saveThread(self,thread):
j= json.dumps(thread, default=lambda thread: thread.__dict__)
self.db.threads.insert(json.loads(j))
def saveForum(self,forum):
j= json.dumps(forum, default=lambda forum: forum.__dict__)
print(j)
self.db.forums.insert(json.loads(j))
def savePost(self,post):
j= json.dumps(post, default=lambda post: post.__dict__)
self.db.posts.insert(json.loads(j))
if __name__=='__main__':
ms = MongoStore()
ms.open()
forum = ForumInfo()
forum.forumId=100
forum.forumName="测试论坛"
ms.saveForum(forum)
post = PostInfo()
post.threadId=1
post.content="hello"
post.postId="2"
ms.savePost(post)