Question

for example..

class Page(Document)
    tags = ListField(StringField())

In this case, we can find out a value in the tags list like this.

Page.objects(tags='coding')

if tags are like ['coding', 'x', 'y'], then the document will be matched...

but My question is how I can find out the value not in the listfield.

my incorrect code would be..

Page.objects(tags!='coding') 

or

Page.objects(tags__not = 'coding')

or

Page.objects(tags__not__in = 'coding')

but.. they don't simply work..

how can I query a document that does not have a given value in a ListField?

Was it helpful?

Solution

To find any pages that don't have the tags coding use the $nin operator:

Page.objects(tags__nin=['coding'])

OTHER TIPS

I would skip using the build-in mongo syntax on this one and just use a raw query:

Page.objects(__raw__={"tags" : {"$ne" : ['coding']}})

As query get more complicated, your going to wish you set it up like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top