简体   繁体   中英

How can I mass query fields with mongoengine?

I am pretty new to mongoengine and MongoDB in general. I am trying to build an advanced search page in Flask with many fields to fill out.

When the search button is pressed Flask will call a function that gets all the arguments for the url, essentially making a python dictionary of the arguments.

Is there a way I can dynamically query MongoDB fields with mongoengine (or any other library) using that dictionary? Either as is, or by putting it in some other data structure?

So, for instance, if the name , set , and color fields were filled out, the equivalent SQL statement I would want would be:

SELECT *
FROM myTable
WHERE `name` = 'nameval' AND `set` = 'setval' AND `color` = 'colorval';

If mongoengine cannot accomplish this, is there one out there that could?

Additionally, I would hard code this, but there are around 25 fields to query, which means there would be a lot of repetition sitting around.

是的,您可以传递要过滤的字典,只要它们以kwargs形式传递即可,例如:

MyModel.objects(**myQueryDictionary)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM