'How to find intersection of array values pymongo
There are 79 parameters in each experiment/document . There are 27 experiment/documents.
{
_id: 0,
experiment: 1,
parameters: [
{
name: "clock",
value: 8,
type: "system"
},
{
name: "B",
value: 100000,
type: "puls"
},
{
name: "campaign",
value: "October2019",
type: "navi"
}
]
}
I would like to have all the experiment/ documents where- name : "B" > value : 1000 and name : "campaign" = value : "October2019"
The output should look like this: https://mongoplayground.net/p/UP39-Yxk61U
Solution 1:[1]
Here is a solution where I use $filter to filter the content of the parameters field
And I use a combination of $and and $or, to recreate your conditions
((name = "B") AND (value > 1000)) OR ((name = "campaign") AND (value = "October2019"))
try it here
db.collection.aggregate([
{
$project: {
_id: 1,
experiment: 1,
parameters: {
$filter: {
input: "$parameters",
as: "parameter",
cond: {
$or: [
{
$and: [
{
$eq: [
"$$parameter.name",
"B"
]
},
{
$gt: [
"$$parameter.value",
1000
]
}
]
},
{
$and: [
{
$eq: [
"$$parameter.name",
"campaign"
]
},
{
$eq: [
"$$parameter.value",
"October2019"
]
}
]
},
]
}
}
},
}
}
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AlexisG |
