Reputation: 2118
I am trying to find records in my mongo collection by applying some sort of a $in
operator but in multiple fields (logical AND)
Assuming I have the following records in a collection named items
:
[
{
"key": 'a';
"value": 'b';
"etc": 'foo';
},
{
"key": 'x';
"value": 'y';
"etc": 'bar';
},
{
"key": 'a';
"value": 'y';
"etc": 'jazz';
}
]
Now I want to execute something like:
db.items.find_all({"key, value": {"$in": [("a", "b"), ("x", "y")]}})
Which would yield the result records:
[
{
"key": 'a';
"value": 'b';
"etc": 'foo';
},
{
"key": 'x';
"value": 'y';
"etc": 'bar';
}
]
Is there anything similar to that? or a way to manipulate $in
to achieve that output?
Thank you very much
Upvotes: 0
Views: 844
Reputation: 37048
$in
is not right tool for the job. https://docs.mongodb.com/manual/reference/operator/query/in/ clearly states it is for single field only.
You should be fine with old plain "or" unless I miss something in your requirements:
the_list = [("a", "b"), ("x", "y")]
db.items.find_all({"$or": [{"key":t[0], "value":t[1]} for t in the_list]} )
Upvotes: 2