Mongo 同时使用多个条件进行筛选的查询
Mongo Query to filter using multiple conditions together
如何查找具有所有值类型或 MongoDB 中某些值的文档?
我想将文档中的一些字段用作过滤器来过滤文档。
例子:-
Person 字段可以采用以下值之一:- [Person A, Person B, Person C,....]
Type 字段可以采用以下值之一:- [Type A, Type B, Type C]
Source 字段可以采用以下值之一:- [Source A, Source B, Source C]
我想结合使用上述字段来过滤文档。
上述字段should be filtered as All or some specific value
的字段
我的意思是
对于查询,我想要A 类 的Source B 的所有人员 .
对于另一个查询,我可能想要 人 B 的 所有类型 对于 来源 C .
等等...
我想使用查询中组合的所有字段来过滤文档。
你是如何在MongoDB中做到这一点的?
数据集:-
{
"_id": "1",
"Person": "Person A",
"type": "Type A",
"source": "Source A",
"subject": "some subject"
},
{
"_id": "2",
"Person": "Person B",
"type": "Type B",
"source": "Source C",
"subject": "some subject"
},
{
"_id": "3",
"Person": "Person C",
"type": "Type A",
"source": "Source A",
"subject": "some subject"
},
{
"_id": "4",
"Person": "Person C",
"type": "Type C",
"source": "Source B",
"subject": "some subject"
},
{
"_id": "5",
"Person": "Person A",
"type": "Type B",
"source": "Source A",
"subject": "some subject"
},
{
"_id": "6",
"Person": "Person B",
"type": "Type C",
"source": "Source A",
"subject": "some subject"
}
示例过滤器查询 1:-
人:全部(即 A、B、C)
类型: A
来源: A
查询 1 的预期输出:-
获取带有 id:- 1 和 3 Coz only ids 1&3 have Type and Source as A
的文档
示例过滤器查询 2:-
人: A
类型:全部
来源:全部
查询 2 的预期输出:-
获取带有 id 的文档:- 1 和 5 coz person A has documents 1 & 5 associated with him.
示例过滤器查询 3:-
个人:全部
类型:全部
来源: B
查询 2 的预期输出:-
获取带有 id 的文档:- 4 coz only id 4 has source as B
在使用 $eq
执行查询时,当提供的参数为“全部”时,您可以使用 $or
来“转义”查询。
db.collection.aggregate([
{
"$addFields": {
// put your query param here
"paramPerson": "All",
"paramType": "Type A",
"paramSource": "Source A"
}
},
{
$match: {
$expr: {
$and: [
{
$or: [
{
$eq: [
"$paramPerson",
"All"
]
},
{
$eq: [
"$paramPerson",
"$Person"
]
}
]
},
{
$or: [
{
$eq: [
"$paramType",
"All"
]
},
{
$eq: [
"$paramType",
"$type"
]
}
]
},
{
$or: [
{
$eq: [
"$paramSource",
"All"
]
},
{
$eq: [
"$paramSource",
"$source"
]
}
]
}
]
}
}
},
{
"$project": {
"paramPerson": false,
"paramSource": false,
"paramType": false
}
}
])
这里是Mongo playground供大家参考。
如何查找具有所有值类型或 MongoDB 中某些值的文档?
我想将文档中的一些字段用作过滤器来过滤文档。
例子:-
Person 字段可以采用以下值之一:- [Person A, Person B, Person C,....]
Type 字段可以采用以下值之一:- [Type A, Type B, Type C]
Source 字段可以采用以下值之一:- [Source A, Source B, Source C]
我想结合使用上述字段来过滤文档。
上述字段should be filtered as All or some specific value
的字段
我的意思是
对于查询,我想要A 类 的Source B 的所有人员 .
对于另一个查询,我可能想要 人 B 的 所有类型 对于 来源 C .
等等...
我想使用查询中组合的所有字段来过滤文档。
你是如何在MongoDB中做到这一点的?
数据集:-
{
"_id": "1",
"Person": "Person A",
"type": "Type A",
"source": "Source A",
"subject": "some subject"
},
{
"_id": "2",
"Person": "Person B",
"type": "Type B",
"source": "Source C",
"subject": "some subject"
},
{
"_id": "3",
"Person": "Person C",
"type": "Type A",
"source": "Source A",
"subject": "some subject"
},
{
"_id": "4",
"Person": "Person C",
"type": "Type C",
"source": "Source B",
"subject": "some subject"
},
{
"_id": "5",
"Person": "Person A",
"type": "Type B",
"source": "Source A",
"subject": "some subject"
},
{
"_id": "6",
"Person": "Person B",
"type": "Type C",
"source": "Source A",
"subject": "some subject"
}
示例过滤器查询 1:-
人:全部(即 A、B、C)
类型: A
来源: A
查询 1 的预期输出:-
获取带有 id:- 1 和 3 Coz only ids 1&3 have Type and Source as A
示例过滤器查询 2:-
人: A
类型:全部
来源:全部
查询 2 的预期输出:-
获取带有 id 的文档:- 1 和 5 coz person A has documents 1 & 5 associated with him.
示例过滤器查询 3:-
个人:全部
类型:全部
来源: B
查询 2 的预期输出:-
获取带有 id 的文档:- 4 coz only id 4 has source as B
在使用 $eq
执行查询时,当提供的参数为“全部”时,您可以使用 $or
来“转义”查询。
db.collection.aggregate([
{
"$addFields": {
// put your query param here
"paramPerson": "All",
"paramType": "Type A",
"paramSource": "Source A"
}
},
{
$match: {
$expr: {
$and: [
{
$or: [
{
$eq: [
"$paramPerson",
"All"
]
},
{
$eq: [
"$paramPerson",
"$Person"
]
}
]
},
{
$or: [
{
$eq: [
"$paramType",
"All"
]
},
{
$eq: [
"$paramType",
"$type"
]
}
]
},
{
$or: [
{
$eq: [
"$paramSource",
"All"
]
},
{
$eq: [
"$paramSource",
"$source"
]
}
]
}
]
}
}
},
{
"$project": {
"paramPerson": false,
"paramSource": false,
"paramType": false
}
}
])
这里是Mongo playground供大家参考。