DB/MongoDB 2015. 3. 16. 11:56

pymongo site : http://api.mongodb.org/python/current/



# ?/usr/bin/python

import pymongo


class Database():

    def __init__(self, address=DB_DEFAULT_ADDRESS, port=DB_DEFAULT_PORT):

        self.port = port

        self.address = address

          

    def getDb(self, dbName=DB_DEFAULT_NAME):

        client = pymongo.MongoClient(self.address, self.port)

        return client[dbName]

    

class DbQuery():

    def __init__(self, db):

        self.db = db

        

    def insert(self, col, val):

        try:

            self.db[col].insert(val)

        except Exception as e:

            return e.args[0]

        

    def remove(self, col, id):

        try:

            self.db[col].remove(where)

        except Exception as e:

            return e.args[0]

    

    def set(self, col, where, val):

        try:

            self.db[col].update(where, {"$set":val})

        except Exception as e:

            return e.args[0]

    

    def push(self, col, where, val):

        try:

            self.db[col].update(where, {"$push": val}, True)

        except Exception as e:

            return e.args[0]

        

    def pull(self, col, where, val):

        try:

            self.db[col].update(where, {"$pull": val}, True)

        except Exception as e:

            return e.args[0]

        

    def find(self, col, where, val):

        try:

            return self.db[col].find(where, val)

        except Exception as e:

            return e.args[0]

        return e.args[0]

    

    def findOne(self, col, where, val):

        try:

            return self.db[col].find_one(where, val)

        except Exception as e:

            return e.args[0]

        return e.args[0]

    

    def inc(self, col, where, val):

        try:

            self.db[col].update(where, {"$inc":val})

        except Exception as e:

            return e.args[0]


'DB > MongoDB' 카테고리의 다른 글

[mongodb] command 요약  (0) 2015.03.16
[mongodb] 데이터 모델링  (0) 2015.03.16
[mongodb] mongo 결과 파일 출력  (0) 2015.03.03
[mongodb] 용어&쿼리비교  (0) 2015.01.07
mongodb 설치  (0) 2015.01.07
posted by cozyboy
: