---
title: "pymongo 在$project 时判断数组是否含有某个元素"
description: "判断 roll 中是否存在_uid:"
canonical_url: "https://yangqiuyi.com/blog/kai-fa/pymongo-zai-project-shi-pan-duan-shu-zu-shi-fou-han-you-mou-ge-yuan-su"
---

> Index of every page on this site: [llms.txt](https://yangqiuyi.com/llms.txt)

# pymongo 在$project 时判断数组是否含有某个元素

判断 `roll` 中是否存在`_uid`:

```python
cls.event.aggregate([
    {
    "$project": {
        "xxx" : 1,
        "joined": {
            '$cond': {
                'if': { "$in": [_uid, "$roll"] },
                "then": True,
                "else": False
            }
        }
    }
    },
    { "$sort": { '_id': -1 } }
])
```

注意点: project操作不可以是exlude模式, 否则会报错

```txt
Cannot use expression other than $meta in exclusion projection
```

## Sitemap

See the full [sitemap](https://yangqiuyi.com/sitemap.md) for all pages.
