【问题标题】:Check if document exists in pymongo检查 pymongo 中是否存在文档
【发布时间】:2020-10-12 03:40:01
【问题描述】:

我正在尝试查看数据库中是否已存在文档 这是我的初始代码:

myclient = pymongo.MongoClient("server link")
mydb = myclient["snkr"]
dblist = myclient.list_database_names()
collist = mydb.list_collection_names()
mycol = mydb["auth_servers"]

所以它需要一个名为 snkr 的数据库和一个名为 auth_servers 的集合,现在为了检查文档是否已经存在,我使用代码:

server_name = ctx.guild
    server_id = ctx.guild.id
    if mycol.count_documents({ 'ServerName':str(server_name), 'ServerID': str(server_id) }, limit = 1):
        await ctx.send(f"This server is already authorized")
    else:
        #code that inserts the value into the DB in case there isn't already one

我尝试了不同的方法:

if mydb.mycol.count_documents({ 'ServerName':str(server_name), 'ServerID': str(server_id) }, limit = 1):

if mydb.count_documents({ 'ServerName':str(server_name), 'ServerID': str(server_id) }, limit = 1):

if mydb.auth_servers.count_documents({ 'ServerName':str(server_name), 'ServerID': str(server_id) }, limit = 1):

if mydb["auth_servers"].count_documents({ 'ServerName':str(server_name), 'ServerID': str(server_id) }, limit = 1):

但它们都不起作用

【问题讨论】:

    标签: python mongodb discord pymongo


    【解决方案1】:

    已解决:

    check = mycol.find({},{'ServerID':"724523958296117328"}).count()
        print(check)
        if check >= 1:
            await ctx.send(f"This server is already authorized")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 2018-12-17
      • 2012-07-02
      • 2019-03-04
      • 2021-11-09
      相关资源
      最近更新 更多