0

With a Python list I can do:

if 'myfakeuser' in list_of_usernames:
    print "This user exists"

How can I do this with Rethinkdb? r.db('mydb').table('users').get('myfakeuser').run() doesn't return anything or raise an error when there's no id set myfakeuser.

2 Answers 2

1

The Python driver behaves the same way as the Ruby driver. If the key does not exist, it will return None.

>>> my_profile = r.table('foo').get('myname').run(conn)
>>> print my_profile
>>> None

Also you can use the is_empty method, which will return True if the value does not exist value or it will return False if the value does exist.

>>> doesnt_exist = r.table('foo').get_all('myname').is_empty().run(conn)
>>> print doesnt_exist
>>> True
0

If the query is correct, and nothing is wrong, no error will be returned, but a nil value will be return instead. Think about SQL, it return an empty set of row instead throwing error, because nothing is wrong with query nor the server connection.

I don't know Python, but in JavaScript or Ruby, a null/nil value will be returned

Run this on Data Exploer and you get a null result

r.table('foo').get('anivalidprimarykey')

Or in JavaScript:

r.table('foo').get('anivalidprimarykey').run(connection)
.then(function(result) {
  console.log('r=', result)
})
.error(function(err) {
  console.log('e=', err)
})

You will get a null value for result variable.

Same thing with Ruby, though you get a nil value.

require 'rethinkdb'
include RethinkDB::Shortcuts
r.connect.repl
r.table(:foo).get('an_in_valid_key').run
nil

So I assume that the Python driver will do same, return a nil value in your language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.