0

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 ]

1 Answers1

0
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)

Running in normal python this works.

This is because you define the MongoDB connection as conn, the db is called mydb and the collection is called mycol.

MongoDB's structure looks like: Connection -> Database -> Collection -> Document -> Key/Value

Your solution would be:

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)
Axisnix
  • 2,822
  • 5
  • 19
  • 41