MongoEngine - Query - How to check if a ListField is empty or not set

Viewed 12214

how do I check if an ListField() attribute of a Mongo Class is not set or empty?

Thanks!

3 Answers

If you were looking for the reverse question (check if a list contains at least an element which implies that it also exists), here is how you can do it using a query dict for a change:

query = {}
query['tags__exists'] = True
query['tags__not__size'] = 0
for post in Post.objects(**query):
    print(post)

As per the MongoEngine documentation, the not operator can be used to negate a standard check as long as it precedes the query operator.

Related