pythonrethinkdbrethinkdb-python

How would I check if a filtered request returns an empty array?


I started Javascript around a year and a half ago but I started Python around a week ago so I'm still a beginner at this. So I'm trying to figure out if a user is already stored into the users table and if they aren't then it will add them to it and if they are then it skips.

import rethinkdb as r


r.db('bot').table('users').filter(r.row["id"] == "253544423110082589").run()

this code should return

<rethinkdb.net.DefaultCursor object at 0x03DAAE10> (done streaming):
[
]

So how exactly would I check if its empty?

I tried something like

if not r.db('bot').table('users').filter(r.row["id"] == "253544423110082589").run():
    # add user to table
else:
    continue

but it continues even when the user isn't in the table. So please tell me what I'm doing wrong and how I can actually get it to work


Solution

  • From the docs use the is_empty function:

    r.db('bot').table('users').filter(r.row["id"] == "253544423110082589").is_empty().run(conn)
    

    You could also use count

    r.db('bot').table('users').filter(r.row["id"] == "253544423110082589").count().run(conn)