1. 提前安装pymongo
pip install pymongo
2. 程序如下:
import time
import pymongo
from urllib import quote_plus
datestr = time.strftime("%Y-%m-%d", time.localtime(time.time()))
dict1 = {"Date":datestr, "FileCounts":1000}
user = "admin"
password = "ZTI1ZDk3ZWFkNmM"
host = "10.110.92.85:9381"
uri = "mongodb://%s:%s@%s" % (quote_plus(user), quote_plus(password), host)
connection=pymongo.MongoClient(uri,9381)
db=connection.get_database("hadoop-edit-logs") #.alpsfiles
collec = db.get_collection("FileCounts")
collec.insert_one(dict1)
connection.close()