File size: 1,153 Bytes
37e6c30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#python -m pip install "pymongo[srv]==3.11"
from pymongo.mongo_client import MongoClient
from datetime import datetime
import os

def insert_mongodb_log(model_name,document:dict):

    mongo_uri = os.getenv('mongo_uri')
    client = MongoClient(mongo_uri)

    try:
        #client.admin.command('ping')
        # 選擇數據庫,如果不存在會自動創建
        db = client["huggingface-space"]  # 替換成你想要的數據庫名稱
        # 選擇集合,如果不存在會自動創建
        collection = db["space-log"]  # 替換成你想要的集合名稱
        # 將文檔插入到集合中
        document.update({"model_name":model_name,"process_time": str(datetime.now())})
        
        result = collection.insert_one(document)
        # 打印插入結果
        #print(f"Document inserted with ID: {result.inserted_id}")
        
    except Exception as e:
        print(e)
    
    
    
if __name__ == "__main__":
    # 創建JSON文檔
    document = {
        "msg": "hello world",
        "status": "success",
    }
    
    insert_mongodb_log("test_client",document)