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

发布于 2022-08-30 00:31:39 字数 200 浏览 21 评论 0

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女猫性小仙女 2022-09-06 00:31:39

其实就用 $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",
    },
})
尸血腥色 2022-09-06 00:31:39
Post.objects(tags__ne='php')
策马西风 2022-09-06 00:31:39

$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.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文