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.

Tim Clemans
  • 875
  • 1
  • 10
  • 18

2 Answers2

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
linuxdynasty
  • 376
  • 2
  • 5
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.

kureikain
  • 2,304
  • 2
  • 14
  • 9