zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    网页信息抓取 Jsoup的不足之处 httpunit
    spring boot @ConfigurationProperties
    git冲突解决方案 Intellij IDEA
    HTTP 和 Socket 区别
    java.lang.reflect.Method
    dubbo hessian+dubbo协议
    4、注解反射生成SQL语句
    3、解析注解
    2、自定义注解
    1、JDK自带注解
  • 原文地址:https://www.cnblogs.com/lshan/p/11855590.html
Copyright © 2011-2022 走看看