Milo Gertjejansen
Milo Gertjejansen

Reputation: 511

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.

Upvotes: 0

Views: 293

Answers (1)

Ross
Ross

Reputation: 18111

Yes you can pass a dictionary to be filtered as long as they are passed as kwargs eg:

MyModel.objects(**myQueryDictionary)

Upvotes: 1

Related Questions