zoukankan      html  css  js  c++  java
  • Python pyMongoDB 基础使用模版

    Python pyMongoDB基础使用 - https://www.cnblogs.com/iAmSoScArEd/p/14903131.html -我超怕的

    当不知道MongoDB中有什么数据库时可使用以下函数查询所有数据库:

    print(client.database_names()) 

    当不知道MongoDB中有什么集合时可使用一下函数获取所有集合

    print(db.collection_names()) 

    from pymongo.mongo_client import MongoClient
    from pymongo import ReadPreference
    
    
    '''Connect the mongoDB'''
    def get_mongo_conn(host, port, user, password, usedatabse):
        db = None
        client = None
        try:
            client = MongoClient(host, port,maxPoolSize=500, connectTimeoutMS=10000,serverSelectionTimeoutMS=5000)
            # print(client.database_names()) 获取所有数据库
            db = client.get_database(usedatabse,read_preference=ReadPreference.SECONDARY)
            
            if db.authenticate(user, password):
                # print('认证成功!')
                # print(db.collection_names()) 获取所有集合
                pass
            else:
                print('认证失败!')
        except Exception as ex:
            print("connection Mongo exception: %s" % ex.message)
        return db, client
    
    # settings
    def get_mongo_config():
        return {
            'host' : '127.0.0.1',
            'port' : 27000,
            'dbName' : '1234',
            'userName' : 'username',
            'password' : 'password',
            'collectionName' : 'collection'
        }
    
    # define function to query one row
    def query_one(queryDict={}):
        # return dict
        gmc = get_mongo_config()
        host = gmc['host']
        port = gmc['port']
        dbName = gmc['dbName']
        userName = gmc['userName']
        password = gmc['password']
        collectionName = gmc['collectionName']
        db,client = get_mongo_conn(host,port,userName,password,dbName)
        if db is None or client is None:
            print('获取mongo连接失败!')
            return None
        collection = db[collectionName]
        return collection.find_one()
    
    # define function to query more rows
    def query_all(queryDict,limit):
        # return list
        gmc = get_mongo_config()
        host = gmc['host']
        port = gmc['port']
        dbName = gmc['dbName']
        userName = gmc['userName']
        password = gmc['password']
        collectionName = gmc['collectionName']
        db,client = get_mongo_conn(host,port,userName,password,dbName)
        if db is None or client is None:
            print('获取mongo连接失败!')
            return None
        collection = db[collectionName]
        return collection.find(queryDict).limit(limit)
    
    def run(device=''):
        queryDict = {} #查询条件
        results = query_all(queryDict,5) # query 5 rows
        for rs in results:
            print(rs)
    
    run()
    俗人昭昭,我独昏昏。俗人察察,我独闷闷。
  • 相关阅读:
    模拟信号的优缺点分析
    PLC控制网关的功能介绍及应用领域
    LoRa无线数传终端的优势
    串口服务器厂家哪家好
    串口转以太网转换器的工作模式
    一个能手机控制水泵的无线远程开关控制器
    以太网IO模块是什么
    支持MQTT的模块有哪些
    常用正交表
    Spring Boot源码(五)以HttpEncodingAutoConfiguration【Http 编码自动配置】为例解释自动配置原理
  • 原文地址:https://www.cnblogs.com/iAmSoScArEd/p/14903131.html
Copyright © 2011-2022 走看看