Mongoengine… query something not in a ListField?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:30:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!