mongodb数据库常用操作的整理

这是个人在项目中抽取的代码,自己写的utils的通用模块,使用的框架是tronado,包括了数据库的认证,以及增删改查排序,如有特别需要可以联系我或者自己扩展,刚学python不久,仅供参考,例子如下。

 

# -*- coding: utf-8 -*-
import pymongo
import logging
from bson.objectid import ObjectId

from etc.config import *
conf_log = logging


class DatabaseAPI(object):
    def __init__(self):
        super(DatabaseAPI, self).__init__()
        self.log = logging

        # MongoDB info set in config
        self.mg_host = mgHost
        self.mg_port = str(mgPort)
        self.mg_username = mgUsername
        self.mg_password = mgPassword
        self.mg_database = mgDatabase
        self.mg_auth_method = mgAuthMethod
        self.mg_client = None
        self.mg_conn = None

        # Login mongoDB
        self.mg_client = pymongo.MongoClient("mongodb://%s:%s" % (mgHost, mgPort))
        self.mg_conn = self.mg_client[self.mg_database]
        if self.mg_username or self.mg_password:
            auth_method = mgAuthMethod if mgAuthMethod else 'DEFAULT'
            self.mg_conn.authenticate(mgUsername, mgPassword, mechanism=auth_method)

    def login_mongodb(self):
        # Login mongodb using or not using authentication
        self.mg_client = pymongo.MongoClient("mongodb://%s:%s" % (mgHost, mgPort))
        self.mg_conn = self.mg_client[self.mg_database]
        if self.mg_username or self.mg_password:
            auth_method = mgAuthMethod if mgAuthMethod else 'DEFAULT'
            self.mg_conn.authenticate(mgUsername, mgPassword, mechanism=auth_method)
        return self.mg_conn

    def std_filter_exp(self, filter_exp):
        # Standardize filter expression
        self.log.error("Filter_exp before modified: " + str(filter_exp))
        if filter_exp:
            if filter_exp.get("_id"):
                if isinstance(filter_exp["_id"], str) \
                        or isinstance(filter_exp["_id"], unicode):
                    filter_exp["_id"] = ObjectId(str(filter_exp["_id"]))
                elif isinstance(filter_exp["_id"], dict):
                    if filter_exp["_id"].get("$in"):
                        filter_exp["_id"]["$in"] = [ObjectId(str(doc_id)) for doc_id in filter_exp["_id"]["$in"]]
        self.log.error("Filter_exp after modified: " + str(filter_exp))
        return filter_exp

    def stdout_documents(self, documents):
        # Standardize content of expression
        self.log.debug("Output before modified: " + str(documents))
        for document in documents:
            if document.get("_id"):
                document["_id"] = str(document["_id"])
        self.log.debug("Output after modified: " + str(documents))
        return documents

    def stdin_documents(self, documents):
        # Standardize content of expression
        self.log.debug("Input before modified: " + str(documents))
        if isinstance(documents, (list, tuple)):
            for document in documents:
                if document.get("_id"):
                    document["_id"] = ObjectId(str(document["_id"]))
            self.log.debug("Input after modified: " + str(documents))
        else:
            documents = [documents]
        return documents

    def mongo_find(self, collection, filter_exp=None, projection=None, skip=0, limit=0, sort=None):
        # Find documents in certain collection
        self.log.debug("MongoDB find: %s, %s, %s, %s, %s, %s" %
                       (str(collection), str(filter_exp), str(projection), str(skip), str(limit), str(sort)))
        mg_col = self.mg_conn[collection]
        filter_exp = self.std_filter_exp(filter_exp)
        result = mg_col.find(filter=filter_exp, projection=projection, skip=skip, limit=limit, sort=sort)
        db_resource = self.stdout_documents([section for section in result])
        return db_resource

    def mongo_insert(self, collection, documents, ordered=False):
        # Insert documents into certain collection
        mg_col = self.mg_conn[collection]
        documents = self.stdin_documents(documents)
        result = mg_col.insert_many(documents, ordered)
        return result

    def mongo_update(self, collection, filter_exp, update_exp, upsert=False):
        # Update documents matching certain filter
        mg_col = self.mg_conn[collection]
        filter_exp = self.std_filter_exp(filter_exp)
        result = mg_col.update_many(filter_exp, update_exp, upsert)
        return result

    def mongo_delete(self, collection, filter_exp):
        # Delete documents matching the filter
        mg_col = self.mg_conn[collection]
        filter_exp = self.std_filter_exp(filter_exp)
        result = mg_col.delete_many(filter_exp)
        return result

 

posted @ 2017-05-09 17:53  jimtong  阅读(553)  评论(0编辑  收藏  举报