pymongo 例子

import pymongo

class dbUtil(object):
    def __init__(self, tablename='functional_testing'):
        con = pymongo.Connection('localhost', 27017)
        self.db = con['test_database']
        self.table = self.db[tablename]

    def save(self, obj_dict):
        self.table.save(obj_dict)

    def insert(self, obj_list):
        ''' insert records in batch or single'''
        self.table.insert(obj_list)

    def get_by_id(self, idd):
        return self.table.find_one({'id':idd})

    def get_count(self, condition={}):
        return self.table.find(condition).count()
        
    def query(self, condition, sort=[("id",pymongo.ASCENDING)]):
        return self.table.find(condition).sort(sort)

    def create_index(self, key_list):
        ''' eg: create_index([("mike", pymongo.DESCENDING), ("eliot", pymongo.ASCENDING)]) '''
        self.table.ensure_index(key_list, unique=True)

    def remove(self, condition={}):
        self.table.remove(condition)
    
    def update(self, condition, data):
        self.table.update(condition, {'$set':data}, multi=True)
        
    def drop_table(self):
        self.table.drop()

    def get_all_collection_names(self):
        print self.db.collection_names()
# db = dbUtil()

 

posted @ 2014-10-15 16:10  道以万计  阅读(640)  评论(0编辑  收藏  举报