问题
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?
回答1:
To find any pages that don't have the tags coding use the $nin operator:
Page.objects(tags__nin=['coding'])
回答2:
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.
来源:https://stackoverflow.com/questions/9565194/mongoengine-query-something-not-in-a-listfield