1. 程式人生 > >【專案實戰】:python:MongoDB資料庫的操作及練習

【專案實戰】:python:MongoDB資料庫的操作及練習

python:MongoDB資料庫的操作及練習

import pymongo

class MongodbConn(object):

    def __init__(self):
        self.CONN = pymongo.MongoClient("mongodb://loan_risk_rw:[email protected]:27001,172.16.10.174:27001/admin?replcaSet=rs001 ")
    def run(self):
        database = "loan_risk_db"
        db = self.CONN[database]
        #db.authenticate("username", "password")
        col = db.list_collection_names()
        # the col is datbases list
        print(col)
        # this is a database, in list loc 4
        col = col[1]
        collection = db.get_collection(col)
        # query one document
        document = collection.find_one()
        print(document)
        # query all document
        documents = collection.find()
        for i in documents:
            # print key of (key: value)
            print (i.keys())

if __name__ == '__main__':
    mongo_obj = MongodbConn()
    mongo_obj.run()