更新时间:2023-11-29 08:25:52
import pymongo
conn = pymongo.MongoClient("mongodb://localhost:27017/")
userID = 21312313
if conn.mydb.mycol.count_documents({ 'userID': userID }):
print("**Error: You're already in the database**")
else:
print("**Adding new inventory to the database**")
mydict = { "userID": userID, "coin": "0", "inv": {"inv1": "", "inv2": "", "inv3": "", "inv4": "", "inv5": ""} }
y = conn.mydb.mycol.insert_one(mydict)
在普通 python 中运行这是有效的.
Running in normal python this works.
这是因为您将 MongoDB 连接定义为 conn
,db 称为 mydb
,集合称为 mycol
.
This is because you define the MongoDB connection as conn
, the db is called mydb
and the collection is called mycol
.
MongoDB 的结构如下:Connection ->数据库 ->集合 ->文档 ->键/值
MongoDB's structure looks like: Connection -> Database -> Collection -> Document -> Key/Value
您的解决方案是:
import pymongo
conn = pymongo.MongoClient("mongodb://localhost:27017/")
userID = ctx.message.author.id
if conn.mydb.mycol.count_documents({ 'userID': userID }):
await ctx.send("**Error: You're already in the database**")
else:
await ctx.send("**Adding new inventory to the database**")
mydict = { "userID": userID, "coin": "0", "inv": {"inv1": "", "inv2": "", "inv3": "", "inv4": "", "inv5": ""} }
y = conn.mydb.mycol.insert_one(mydict)