Reputation: 3060
I have a field in my document place_names which is a list of all possible place names for a location. Example New York City with have New York City, NYC, big apple etc. I want the user to be able to query on any of these values or any part of the above values. For example if they search for "apple" i want them to get New York City back. I was trying to use the __contains filter in mongoengine as below
place_names is of type ListField()
pn = request.POST.get('place_name', None)
try:
places_list = Places.objects()
if pn is not None and pn != "":
places_list.filter(place_names__contains = pn)
In the above example the filter doesn't work the way I expect it to. It works as a regular filter and doesn't do the "_contains". The same filter works fine if the type is StringField(). Is it possible to use "_contains" with ListFields? If not is there any way around this? thanks :)
Upvotes: 0
Views: 3176
Reputation: 18111
__contains
is a string lookup using a regex under the hood. To check if an item is in a listfield you should use the __in
however, that does an exact match.
You could denormalise and create a ListField
with the place names split into single words and lowercased, then you can use __in
to determine if there is a match.
Upvotes: 2