I've tried to learn MongoDB [and more specifically pymongo] and I'm trying to use it in conjunction with discord.py to make an inventory system for a bot. The bot fetches a userID and should then compare it in the database, so that it can append their "inventory" if they are already in it, instead of making an entirely new one.
However, every solution I've tried so far always seems to fail - no matter if a userID is in the database or not, it'll just create a new entry in the database with the same userID.
Solutions I've tried so far:
userID = ctx.message.author.id
if mycol.collection.count_documents({ 'userID': userID }, limit = 1):
await ctx.send("**Adding new inventory to the database**")
mydict = { "userID": userID, "coin": "0", "inv": {"inv1": "", "inv2": "", "inv3": "", "inv4": "", "inv5": ""} }
y = mycol.insert_one(mydict)
else:
await ctx.send("**Error: You're already in the database**")
userID = ctx.message.author.id
result = mydb.find({"userID": userID})
if result.count() > 0:
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 = mycol.insert_one(mydict)
userID = ctx.message.author.id
search = mycol.find({'userID': userID})
if search == True:
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 = mycol.insert_one(mydict)
Is there something I'm doing wrong? Are these solutions just out of date? Any and all help is appreciated.
[Original solutions: https://stackoverflow.com/questions/25163658/mongodb-return-true-if-document-exists ]