【数据库】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()

你可能感兴趣的:(开发工具:MongoDB)