貌似官方文档有点国语简略
save
col = mdb[mdb_config_col].find_one({})
if col is not None:
id = col['_id']
city_key['_id'] = id
mdb[mdb_config_col].save(city_key)
else:
auth
# connect to mongo
mongo_db = MongoClient(mdb_host, mdb_port)
mdb = mongo_db[mdb_db]
if mdb_user:
mdb.authenticate(mdb_user, mdb_pwd)
pymongo 的一个单例模式
from common import config
from pymongo import MongoClient
class Mongo():
__instance = None
def __init__(self):
app_config = config.DevelopmentConfig()
mdb_url = app_config.MDB_URL
self.mdb_client = MongoClient(mdb_url)
self.mdb = self.mdb_client[app_config.MDB]
print 'mongo init....'
@staticmethod
def getInstance():
if Mongo.__instance is None:
Mongo.__instance = Mongo()
return Mongo.__instance
调用的时候通过静态方法getInstance()获取