1

I would like to search my database for a value. I know you can do this for a key with db.search() but there does not seem to be any kind of similar function for searching for a value

I've tried using the contains() function but I have the same issue. It checks if the key is contained in the database. I would like to know if a certain value is contained in the database.

I would like to do something like this that would search for values in tinydb

db.search('value')

If I was able to execute the above command and get the value(if it does exist) or Nothing if it doesn't that would be ideal. Alternatively, if the able returned True or False accordingly, that would be fine as well

CyK
  • 31
  • 3

2 Answers2

1

I'm new here (doing some reading to see if TinyDB would even be applicable for my use case) so perhaps wrong, and also aware that this question is a little old. But I wonder if you can't address this by iterating over each field and searching within for your value. Then, you couldget the key or field wherein a value match was located.

jaymullr
  • 353
  • 1
  • 2
  • 10
  • I'm thinking something like outlined here: https://stackoverflow.com/questions/27197052/is-it-possible-to-ask-for-keys-from-tinydb – jaymullr Feb 18 '20 at 20:48
0

I don't know if this is what you are looking for but which the following command you can check for a specific field value:

from tinydb import Query
User = Query()
db.search(User.field_name == 'value')
Alireza HI
  • 1,873
  • 1
  • 12
  • 20
  • See I don't want to have to use the key to find the value. If you know both the key and value, what's the point of using the database? I just want something simple which I could input the value and it would return true if it existed or false otherwise – CyK Jul 31 '19 at 17:29
  • The usage of knowing key and value is to find the other related keys and values. When you dont know the iey you wouldnt be able to find the value because you hadnt specify where to search for it – Alireza HI Jul 31 '19 at 17:55
  • why can't it search the entire database? For example, db.search(where('key')) searches the entire database for a matching key. I would like the exact same thing but for values – CyK Jul 31 '19 at 23:38