Mongoengine 怎样查询 ListField 里 不包含某个 value 所有结果

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

tags 可以是 ["php", "python", "perl"],还可以是 ["ruby", "java"] 之类的

假设,我要列出 tags 里所有不包含 php 的 post,应该怎么写查询?

阅读 11.2k
3 个回答

其实就用 $ne 就好了。

from pymongo import MongoClient

client = MongoClient("mongodb://127.0.0.1")
db = client["your-db"]
collection = db["your-collection"]
iter = collection.find({
    "tags": {
        "$ne": "php",
    },
})
Post.objects(tags__ne='php')

$nin

Consider the following query:

db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )

If the field holds an array, then the $nin operator selects the documents whose field holds an array with no element equal to a value in the specified array (e.g. , , etc.).

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题