Mongoengine ... request something not in ListField?

eg..

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

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

 Page.objects(tags='coding') 

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

but my question is how can I find out the value not in the list box.

my wrong code will be ...

 Page.objects(tags!='coding') 

or

 Page.objects(tags__not = 'coding') 

or

 Page.objects(tags__not__in = 'coding') 

but .. they do not just work.

How can I request a document that does not have a given value in a ListField?

+7
source share
2 answers

To find any pages that do not have a tag encoding, use the $ nin operator:

 Page.objects(tags__nin=['coding']) 
+15
source

I would skip using the mongo built-in syntax on this and just use the raw request:

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

As the request becomes more complex, you will want to configure it this way.

+1
source

All Articles