Mongo:如何在另一个搜索中使用搜索结果



我当前有一个请求正在执行一些搜索操作($match、$sort、$group,然后是另一个$match(。样本响应:

[
{
"_id": {
"actor": "fcc0895c-ba04-3078-92b7-3328a283a93a",
"level": 30
}
},
{
"_id": {
"actor": "58b84177-5354-3a69-938f-449991e75e49",
"level": 30
}
},
{
"_id": {
"actor": "d8bcb9d8-e8cb-3796-8d78-fca4d63054fc",
"level": 30
}
},
{
"_id": {
"actor": "8a6287ea-7498-8a21-0174-9ac4c64c23e1",
"level": 30
}
},
{
"_id": {
"actor": "af266e72-4455-39b4-a64c-586055358962",
"level": 30
}
},
{
"_id": {
"actor": "8a62879f-6aac-dea3-016a-b53d9bd96365",
"level": 30
}
}

]

我必须完成我的查询,以便进行另一项研究,在该研究中;演员;属性

我尝试了"$查找";钥匙,但不确定这是好方法。。。我怎样才能做到这一点?:(

您的解决方案可能只需要再进行一次自查找。

db.collection.aggregate([
{
"$match": {
"$and": [
{
"skill-id": "03194859-a8d4-4eda-89e3-09beab3cf336"
},
{
"event-type": "evaluated"
},
{
"assessment-type": "TARGET"
}
]
}
},
{
"$sort": {
"actor": -1
}
},
{
"$group": {
"_id": {
"actor": "$actor",
"target": "$statement-level"
}
}
},
{
"$match": {
"_id.target": 30
}
},
{
"$lookup": {
from: "collection",
let: {
actor: "$_id.actor"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$actor",
"$$actor"
]
},
{
$eq: [
"$skill-id",
"03194859-a8d4-4eda-89e3-09beab3cf336"
]
},
{
$eq: [
"$assessment-type",
"CURRENT"
]
}
]
}
}
}
],
as: "currentLookup"
}
},
{
"$unwind": {
path: "$currentLookup",
preserveNullAndEmptyArrays: true
}
},
{
$project: {
_id: 1,
currentSkillLevel: "$currentLookup.statement-level"
}
}
])

这是mongo游乐场供您参考。

请注意,它使用了不相关的子查询,这只适用于MongoDB 3.6+

最新更新