如何检查 MongoDB 数据库中是否已存在某些内容? [英] How can I check if something already exists in a MongoDB database?

查看:102
本文介绍了如何检查 MongoDB 数据库中是否已存在某些内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试学习 MongoDB [更具体地说是 pymongo],并且尝试将它与 discord.py 结合使用来为机器人制作库存系统.机器人获取一个用户 ID,然后应该在数据库中进行比较,以便它可以将他们的库存"附加到用户 ID 中.如果他们已经在里面,而不是制作一个全新的.

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.

然而,到目前为止,我尝试过的每个解决方案似乎都失败了——无论用户 ID 是否在数据库中,它只会在数据库中创建一个具有相同用户 ID 的新条目.

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.

[原解决方案:https://stackoverflow.com/questions/25163658/mongodb-return-true-if-document-exists]

[Original solutions: https://stackoverflow.com/questions/25163658/mongodb-return-true-if-document-exists ]

推荐答案

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)

这篇关于如何检查 MongoDB 数据库中是否已存在某些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆