点击(此处)折叠或打开
- #!/usr/bin/env python
- # -*- encoding:utf-8 -*-
- from pymongo import Connection
- from config import database
- import functools
- class Mongodb(object):
- '''数据库操作辅助类'''
- def __init__(self):
- self.db = None
- def conn(self):
- if not self.db:
- connection = Connection(database.database['dbhost'])
- self.db = connection[database.database['dbname']]
- # from bottle import
- # connection = Connection(bottle.config['database.dbhost'])
- # self.db = connection[bottle.config['database.dbname']]
- return self.db
- def find_one(self, name):
- def wrapper(callback):
- def wrapper(*args, **kwargs):
- conn = self.conn()
- query = args[0]
- if '_id' in query:
- from bson.objectid import ObjectId
- if not isinstance(query['_id'], ObjectId):
- query['_id'] = ObjectId(query['_id'])
- return conn[name].find_one(query)
- return wrapper
- return wrapper
- def find(self, name, select=None, dump=False):
- def wrapper(callback):
- def wrapper(*args, **kwargs):
- conn = self.conn()
- query = args[0]
- cursor = conn[name].find(query, select)
- if 'limit' in kwargs: #limit
- #print 'limit %s' % kwargs['limit']
- cursor.limit(int(kwargs['limit']))
- if 'skip' in kwargs: #skip
- #print 'skip %s' % kwargs['skip']
- cursor.skip(int(kwargs['skip']))
- if 'order' in kwargs:
- order = kwargs['order']
- if isinstance(order, dict):
- for k in kwargs['order']:
- cursor.sort(k, int(kwargs['order'][k]))
- elif isinstance(order, tuple):
- for k, v in p:
- cursor.sort(k, int(v))
- return [record for record in cursor] if dump or 'dump' in kwargs and kwargs['dump'] else cursor
- return wrapper
- return wrapper
- def save(self, name):
- def wrapper(callback):
- def wrapper(*args, **kwargs):
- conn = self.conn()
- obj = args[0]
- if not obj:
- raise ValueError("Not to be save null object")
- if 'addtime' not in obj:
- import datetime
- obj['addtime'] = datetime.datetime.now()
- if '_id' in obj:
- from bson.objectid import ObjectId
- if not isinstance(obj['_id'], ObjectId):
- obj['_id'] = ObjectId(obj['_id'])
- return conn[name].save(obj)
- return wrapper
- return wrapper
- def remove(self, name):
- def wrapper(callback):
- def wrapper(*args, **kwargs):
- conn = self.conn()
- query = args[0]
- if not query: return
- if '_id' in query:
- from bson.objectid import ObjectId
- if not isinstance(query['_id'], ObjectId):
- query['_id'] = ObjectId(query['_id'])
- return conn[name].remove(query,safe=True)['n']
- return wrapper
- return wrapper
- # def make_mongodb_wrapper(name):
- # @functools.wraps(getattr(Mongodb, name))
- # def wrapper(*a, **ka):
- # return getattr(Mongodb(), name)(*a, **ka)
- # return wrapper
- mongo = Mongodb()
用例
点击(此处)折叠或打开
- import mongo as m
- entity = {}
- entity['name'] = "username"
- save(entity)
- @m.find(name='userTable')
- def find(condition, **kwargs):pass
- @m.remove(name='userTable')
- def delete(condition,**kwargs):pass
- @m.save(name='userTable')
- def save(item):pass
- @m.find_one(name='userTable')
- def find_one(condition):pass