程式語言 - Python - v3.x - Connect MongoDB



import os
import sys
import gridfs
from pymongo import MongoClient

DB = 'xxx'
USER = 'xxx'
PASSWD = 'xxx'
IP = 'xxx.xxx.xxx.xxx'

server = MongoClient(host=IP)
db = server[DB]
db.authenticate(USER, PASSWD)
fs = gridfs.GridFS(db)
ret = fs.exists(_id=file_name)
fs.put(file_data, _id=file_name)
file_data = fs.get_version(_id=file_name).read()
db.logout()

P.S. 使用_id儲存才不會有多份相同資料的問題