Closed aseba closed 1 month ago
I think you could combine .exists()
with the NOT operator:
from tinydb import TinyDB, Query
db = TinyDB('test.json')
db.truncate()
db.insert({'foo': [{'name': 'bar'}]})
db.insert({'bar': [{'name': 'foo'}]})
query = Query()
# Where `foo` does NOT exist
result = db.search(~query.foo.exists())
# Print the result
print(result)
If this doesn't work, please leave a comment!
I'm looking into a way to search for all documents that do not have a specific key set. Is there a way to do a search like
Query().f1.exists() == False
ornot Query().f1.exists()
? How can I find all documents that do not have a specific key?A solution I found is to write my own
not_exist
function, but by looking at the code, all query functions callself._generate_test
which I can't call in this case because_generate_test
literallyreturns False
if there is aKeyError
exception, which renders it useless to check for the exact opposite π