MongoDB Compass:如何过滤 MongoDB 文档中嵌入的数组对象
MongoDB Compass: How to filter embedded array object in the MongoDB document
我想根据 importkeyid 和 MissingPersonIds.PhotoId 数组字段过滤文档。 MissingPersonIds.PhotoId 上的过滤器应该像 'sql - like' 运算符一样工作。
架构:
[{
"_id": {
"$oid": "61ada7da9a30fd8471869bbc"
},
"ImportKeyId": 5843,
"Name" : "AV"
"MissingPersonIds": [
{
"PhotoId": "2 - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy - Copy (2).jpg",
"Description": "Account ID not found"
}
]
},
{
"_id": {
"$oid": "619cd7d2181999c9a4da790a"
},
"ImportKeyId": 5753,
"Name" : 'av1'
"MissingPersonIds": [
{
"PhotoId": "94578.jpg",
"Description": "Photo id is not found"
},
{
"PhotoId": "371426759.jpg",
"Description": "Photo id is not found"
}
]
}
.
.
.
]
基于以下条件:
过滤器:{ "ImportKeyId" : 5843 }
投影:{ MissingPersonIds : { $slice:[0,5] }, "MissingPersonIds": { $elemMatch: { PhotoId : { $regex: /202020 /i } } } }
我期待低于输出但 $elemMatch returns 只有一个匹配的记录
[{
"_id": {
"$oid": "61ada7da9a30fd8471869bbc"
},
"ImportKeyId": 5843,
"Name" : "AV"
"MissingPersonIds": [
{
"PhotoId": "202020 - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy - Copy.jpg",
"Description": "Account ID not found"
}
]
}]
我应该用什么代替 $elemMatch
?
可能是 this
db.collection.aggregate([
{
"$match": {
"ImportKeyId": 5843
}
},
{
"$project": {
"_id": 1,
"ImportKeyId": 1,
MissingPersonIds: {
$filter: {
input: "$MissingPersonIds",
as: "item",
cond: {
$gt: [
{
$indexOfCP: [
{
$toLower: "$$item.PhotoId"
},
"202020 "
]
},
-1
]
}
}
}
}
}
])
输出:
[
{
"_id": ObjectId("61ada7da9a30fd8471869bbc")
"ImportKeyId": 5843,
"MissingPersonIds": [
{
"Description": "Account ID not found",
"PhotoId": "202020 - Copy (2).jpg"
},
{
"Description": "Account ID not found",
"PhotoId": "202020 - Copy - Copy.jpg"
},
{
"Description": "Account ID not found",
"PhotoId": "202020 - Copy - Copy (2).jpg"
}
],
}
]
解释:
- 在匹配阶段,您仅过滤具有 ImportKeyId 的文档:5843
- 在项目阶段,您将 _id 和 ImportKeyId 添加到新字段 MissingPersonIds 中,该字段将过滤仅包含字符串的数组中的元素,例如 SQL 中具有“%202020 %”的字符串
我想根据 importkeyid 和 MissingPersonIds.PhotoId 数组字段过滤文档。 MissingPersonIds.PhotoId 上的过滤器应该像 'sql - like' 运算符一样工作。
架构:
[{
"_id": {
"$oid": "61ada7da9a30fd8471869bbc"
},
"ImportKeyId": 5843,
"Name" : "AV"
"MissingPersonIds": [
{
"PhotoId": "2 - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy - Copy (2).jpg",
"Description": "Account ID not found"
}
]
},
{
"_id": {
"$oid": "619cd7d2181999c9a4da790a"
},
"ImportKeyId": 5753,
"Name" : 'av1'
"MissingPersonIds": [
{
"PhotoId": "94578.jpg",
"Description": "Photo id is not found"
},
{
"PhotoId": "371426759.jpg",
"Description": "Photo id is not found"
}
]
}
.
.
.
]
基于以下条件:
过滤器:{ "ImportKeyId" : 5843 }
投影:{ MissingPersonIds : { $slice:[0,5] }, "MissingPersonIds": { $elemMatch: { PhotoId : { $regex: /202020 /i } } } }
我期待低于输出但 $elemMatch returns 只有一个匹配的记录
[{
"_id": {
"$oid": "61ada7da9a30fd8471869bbc"
},
"ImportKeyId": 5843,
"Name" : "AV"
"MissingPersonIds": [
{
"PhotoId": "202020 - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "202020 - Copy - Copy.jpg",
"Description": "Account ID not found"
}
]
}]
我应该用什么代替 $elemMatch
?
可能是 this
db.collection.aggregate([
{
"$match": {
"ImportKeyId": 5843
}
},
{
"$project": {
"_id": 1,
"ImportKeyId": 1,
MissingPersonIds: {
$filter: {
input: "$MissingPersonIds",
as: "item",
cond: {
$gt: [
{
$indexOfCP: [
{
$toLower: "$$item.PhotoId"
},
"202020 "
]
},
-1
]
}
}
}
}
}
])
输出:
[
{
"_id": ObjectId("61ada7da9a30fd8471869bbc")
"ImportKeyId": 5843,
"MissingPersonIds": [
{
"Description": "Account ID not found",
"PhotoId": "202020 - Copy (2).jpg"
},
{
"Description": "Account ID not found",
"PhotoId": "202020 - Copy - Copy.jpg"
},
{
"Description": "Account ID not found",
"PhotoId": "202020 - Copy - Copy (2).jpg"
}
],
}
]
解释:
- 在匹配阶段,您仅过滤具有 ImportKeyId 的文档:5843
- 在项目阶段,您将 _id 和 ImportKeyId 添加到新字段 MissingPersonIds 中,该字段将过滤仅包含字符串的数组中的元素,例如 SQL 中具有“%202020 %”的字符串