MongoDB (pymongo): $in for fields "tuples" 有多种可能性

MongoDB (pymongo): $in for fields "tuples" with multiple possibilities

我试图通过应用某种 $in 运算符但在多个字段中(逻辑与)

在我的 mongo 集合中查找记录

示例:

假设我在名为 items 的集合中有以下记录:

[
    {
        "key": 'a';
        "value": 'b';
        "etc": 'foo';
    },
    {
        "key": 'x';
        "value": 'y';
        "etc": 'bar';
    },
    {
        "key": 'a';
        "value": 'y';
        "etc": 'jazz';
    }
]

现在我想执行如下操作:

db.items.find_all({"key, value": {"$in": [("a", "b"), ("x", "y")]}})

这将产生结果记录:

[
    {
        "key": 'a';
        "value": 'b';
        "etc": 'foo';
    },
    {
        "key": 'x';
        "value": 'y';
        "etc": 'bar';
    }
]

有类似的吗?或操纵 $in 以实现该输出的方法? 非常感谢

$in 不是适合这项工作的工具。 https://docs.mongodb.com/manual/reference/operator/query/in/ 明确指出它仅适用于单个字段。

除非我遗漏了您的要求中的某些内容,否则您应该可以使用旧的普通“或”:

the_list = [("a", "b"), ("x", "y")]
db.items.find_all({"$or": [{"key":t[0], "value":t[1]} for t in the_list]} )