Hi I am trying to make a parametric search using tinydb however I am having difficulty doing 2 things. the first is that I want to specify which fields I want to look at and what values I want to return. for example if I have the following:
db.insert({'name': 'John', 'age': 22})
db.insert({'name': 'John', 'age': 23})
db.insert({'name': 'John', 'age': 24})
db.insert({'name': 'bill', 'age': 2})
db.insert({'name': 'Bob', 'age': 34})
db.insert({'name': 'Jill', 'age': 85})
db.insert({'name': 'John', 'age': 35})
db.insert({'name': 'John', 'age': 42})
and I want to look at the fields name and and age. I want to find john or Jill. And I want to find the ages 22, 23, 85. While I know how to find instances of the age I do not know how to handle the fields (John or Jill). What I am trying to do is this:
def find(self, field, x):
'''
Example usage: print(db.find(['type'], ['apple']))
:param field: list(str) database 'columns' to look at
:param x: list(str) values in cols to look for
:return: list[dic{}]
'''
return self.db.search(self.q[tuple(field)].one_of(x))
# so in my case I want to input something like:
print(class.find(["Jill", "John"], [22, 23, 85]))
The second thing I am trying to figure out is how to look at only the first n results. Doesnt matter if the item order (inserted into the db) is first to last or last to first. for example I want to get two items at a time so it would be {'name': 'John', 'age': 22} and{'name': 'John', 'age': 23} (or john age 35 and 42).
Can you help me do this with tinydb? I am basically using tinydb instead of sqlite because I want to use nested lists ie, {'name': 'John', 'age': [23,22]}.