MCPcopy Create free account
hub / github.com/Boris-code/feapder / MongoDB

Class MongoDB

feapder/db/mongodb.py:24–489  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22
23
24class MongoDB:
25 def __init__(
26 self,
27 ip=None,
28 port=None,
29 db=None,
30 user_name=None,
31 user_pass=None,
32 url=None,
33 **kwargs,
34 ):
35 if not ip:
36 ip = setting.MONGO_IP
37 if not port:
38 port = setting.MONGO_PORT
39 if not db:
40 db = setting.MONGO_DB
41 if not user_name:
42 user_name = setting.MONGO_USER_NAME
43 if not user_pass:
44 user_pass = setting.MONGO_USER_PASS
45 if not url:
46 url = setting.MONGO_URL
47
48 if url:
49 self.client = MongoClient(url, **kwargs)
50 else:
51 self.client = MongoClient(
52 host=ip, port=port, username=user_name, password=user_pass, **kwargs
53 )
54
55 self.db = self.get_database(db)
56
57 # 缓存索引信息
58 self.__index__cached = {}
59
60 @classmethod
61 def from_url(cls, url, **kwargs):
62 """
63 Args:
64 url: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
65 参考:http://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientURI.html
66 **kwargs:
67
68 Returns:
69
70 """
71 url_parsed = parse.urlparse(url)
72
73 db_type = url_parsed.scheme.strip()
74 if db_type != "mongodb":
75 raise Exception(
76 "url error, expect mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]], but get {}".format(
77 url
78 )
79 )
80
81 return cls(url=url, **kwargs)

Callers 2

to_dbMethod · 0.90
mongodb.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected