• mongo utils python


    #encoding=utf-8
    #!/usr/bin/python3

    from pymongo import MongoClient
    import logging

    
    

    myclient =MongoClient("mongodb://192.168.18.129:27017")
    # client = MongoClient(MongoDB_HOST, MongoDB_HOST_PORT)
    # mongo_host="192.168.18.129"
    mongo_database="MYdataBase"
    mongo_collection="mytable"

    
    

    def insert_one(conntent_dist,collection=mongo_collection,database=mongo_database):
    try:
    mydb = myclient[database]
    mycol = mydb[collection]
    #add auth
    mydb.authenticate("mongodb", "ASSS")
    mycol.insert_one(conntent_dist)
    logging.info("save mongoDB ok")
    except Exception as e:
    logging.error("save mongoDB fail and exception is "+str(e))

    
    
    
    def insert_list(collection,conntent_list,database=mongo_database):
        mydb = myclient[database]
        mydb.authenticate("mongodb", "ASSS")
        mycol = mydb[collection]
        mycol.insert_many(conntent_list)
        print("save list ok")
    
    
    
    
    if __name__ == '__main__':
        collection="ggg"
        conntent_dist={"name": "SeatestPy", "addr": "sz", "url": "https://www.123.com"}
        insert_one(conntent_dist)
        mylist = [
          { "_id": 5, "name": "xiaofang", "cn_name": "消防"}, #如果指定了id ,那么会覆盖
          { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" },
          { "name": "Github", "alexa": "109", "url": "https://www.github.com" }
                ]
    #     insert_list(collection, mylist)
  • 相关阅读:
    申请奖励加分
    6.14
    6.11
    6.10
    6.9
    6.8
    6.7
    6.6
    6.5
    6.4
  • 原文地址:https://www.cnblogs.com/lshan/p/11855590.html
Copyright © 2020-2023  润新知