我需要帮助来对数据进行 mongo 查询
Employee{
history:{
include:
[{type,value},{type,value}]
},
exclude:
[{type,value},{type,value}]
}
}
在上面的数据中,我需要获取包含 include.type C 或 P 且值为 1,2,3 的员工,并且不应包含 except.type A 或 B 值为 2,4,6
我使用了以下查询
$and: [
{
$or: [
{ "history.include.type": "C", "history.include.value": { $in: [1, 2, 3] } },
{ "history.include.type": "P", "history.include.value": { $in: [1, 2, 3] } }
]
},
{
$or: [
{ "history.exclude.type": "A", "history.exclude.value": { $nin: [2, 4, 6] } },
{ "history.exclude.type": "B", "history.exclude.value": { $nin: [2, 4, 6] } }
]
}
]
})
Sample Data:
Employee{
history:{
include:[
{
type:A,
value:1
},
{
type:B,
value:2
}
]},
exclude:[
{
type:C,
value:1
}
]}
}
I have records matching the criteria but the query does not return teh desired output
db.employees.aggregate([
{
$match: {
$and: [
{
"history.include": {
$all: [
{ $elemMatch: { type: "C", value: { $in: [1, 2, 3] } } },
{ $elemMatch: { type: "P", value: { $in: [1, 2, 3] } } }
]
}
},
{
"history.exclude": {
$not: {
$elemMatch: {
$or: [
{ type: "A", value: { $in: [2, 4, 6] } },
{ type: "B", value: { $in: [2, 4, 6] } }
]
}
}
}
}
]
}
}
])