5 Star 1 Fork 0

王泽华/高级软件工程

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DBController.py 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
import pymongo
# MongoDB connection details
mongo_url = "mongodb://localhost:27017/"
db_name = "steam"
collection_name = "Items"
# Save data in the database
def save_data(data):
client = pymongo.MongoClient(mongo_url)
# print(client.list_database_names())
db = client[db_name]
collection = db[collection_name]
documents = collection.find()
for item in data:
item["_id"] = item["name"] # Set the item name as the _id
# Check if the item already exists in the database
if collection.count_documents({"_id": item["_id"]}) == 0:
collection.insert_one(item)
else:
collection.update_one({"_id": item["_id"]}, {"$set": item})
client.close()
# Select all documents from the "Items" table
def select_all():
client = pymongo.MongoClient(mongo_url)
db = client[db_name]
collection = db[collection_name]
documents = collection.find()
for document in documents:
print(document)
client.close()
# Example usage
# data = [
# {"name": "item1", "price": 56},
# {"name": "item2", "price": 20},
# {"name": "item4", "price": 30}
# ]
# save_data(data)
# select_all()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rexwangzehua/advanced-software-engineering.git
git@gitee.com:rexwangzehua/advanced-software-engineering.git
rexwangzehua
advanced-software-engineering
高级软件工程
master

搜索帮助