answerquest , you actually have the ids in the search results. The search method returns you a list of found docs. When you print it out you see the values for the doc. But each doc is a class of type tinydb.database.Document. It has an attr called doc_id.
below is a example as its hard for me to explain. also the bottom of this thread -
https://github.com/msiemens/tinydb/issues/199 might be useful.
Hope it helps
from tinydb import TinyDB, where, __version__ as tdb_version
from faker import Faker
fake = Faker()
def make_dummy_data(num_items=100):
'''
Create some dummy people to use...
returns a list of dicts
built up using Faker
'''
return [
{'idx': i,
'first': fake.first_name(),
'last': fake.last_name(),
'age': fake.random_int(min=18, max=106),
'some_date':fake.date(pattern="%Y-%m-%d"),
}
for i in range(0, num_items)
]
db_fn = 'dummydata.json'
db = TinyDB(db_fn)
db.purge_tables()
data = make_dummy_data()
db.insert_multiple(data)
docs = db.search(where('age') > 50)
if not docs:
print('No docs found, och....')
exit(0)
'''
although doc appears to be a dict, its actually a class -
class 'tinydb.database.Document'
It subclasses dict, also has a 'doc_id' instance member
The class is defined at the top of the database.py source file
'''
print(type(docs[0]))
print(dir(docs[0]))
# can uncomment line below to see more detail
#print(help(type(docs[0])))
# iterate over the docs returned from search
for doc in docs:
print(doc.doc_id, doc['age']) # just print doc_id and age