Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

is it possile to pass a list to a query field?#495

Unanswered
Danelfcf asked this question inQ&A
Discussion options

Hi I am trying to use tinydb for a parametic search, however I am having a hard time with the Query. what I want to do is the following:
I have a database created like db.insert({'name': 'John', 'age': 22, 'job'='student'}) and I want to setup a search where I only look for the fields name and age. while I can do this by hand I want to be able to pass a list of feilds and a list of values like the following:

deffind(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{}]        '''returnself.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]))

while I know how to handle checking for the values using one_of, I cannot find the equivalent command for the 'field'.

You must be logged in to vote

Replies: 1 comment

Comment options

Hey@Danelfcf,

I'm not sure what you mean to do with theself.q[tuple(field)] call. According to your example code,field is a list of strings. And if I understand correctly, you want to return all documents where at least one of the fields includes one of the values passed inx. Is that correct? If so, I got something to work like this:

fromtinydbimportTinyDB,Querydb=TinyDB('test.json')db.truncate()db.insert({'name':'John','age':22,'job':'student'})db.insert({'name':'John1','age':23,'job':'student'})db.insert({'name':'John2','age':24,'job':'student'})db.insert({'name':'John3','age':25,'job':'student'})deffind(fields,x):'''    Example usage: print(db.find(['type'], ['apple']))    :param fields: list(str) database 'columns' to look at    :param x: list(str) values in cols to look for    :return: list[dic{}]    '''query=Query()[fields.pop(0)].one_of(x)forfieldinfields:query=query|Query()[field].one_of(x)returndb.search(query)print(find(['age','name'], [22,23,'John3']))
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@Danelfcf@msiemens

[8]ページ先頭

©2009-2025 Movatter.jp