【发布时间】:2019-06-20 14:57:42
【问题描述】:
我有这两个集合,我想作为 2 个条件加入。examId 和用户 ID。我使用的是 mongodb 版本 4 和 nodejs。我的应用程序中没有猫鼬 考试:
{"_id" : ObjectId("5c4c21d8f45b3d53ff8c20a3"),
"title" : "first azmoon",
"time" : 600,
"preLesson" : {
"label" : "Lesson 13",
"value" : ObjectId("5c484e1852faa438c36c3da1")
},
}
结果:
{
"_id" : ObjectId("5c4993d5d0dc6f39c0f324bd"),
"usrId" : ObjectId("5c484e8852faa438c36c3da2"),
"lsnId" : ObjectId("5c484e1852faa438c36c3da1"),
"passedLesson" : false,
"timePassed" : "",
"quiz" : {
"time" : "5",
"questionTrue" : 0,
"getScore" : 0,
"permission" : true,
"quizScore" : 15,
"quizCount" : 3
},
"exam" : {
"examScore" : 0,
"examCount" : 0,
"getScore" : 0,
"time" : 600,
"questionTrue" : 0,
"permission" : false,
"exId" : ObjectId("5c4c21d8f45b3d53ff8c20a3")
}
}
我想要它的psodou SQL代码的结果是这样的:
select * from exams left join result on exam._id = result.exam.exaId
where result.usrId = ObjectId("5c484e8852faa438c36c3da2")
我做了以下,但它只加入考试和结果,没有任何条件
con.collection("exam").aggregate([
{
$lookup: {
from: "result",
let: {exaId: "$_id"},
pipeline: [
{
$match: {
$expr: {
$and: [
{$eq: ["exam.exId", "exaId"]},
{"usrId": new
ObjectID(`${usrId}`)}
]
}
}
},
],
as: "result"
}
},
{
$lookup: {
from: "lesson",
localField: "preLesson.value",
foreignField: "_id",
as: "lesson"
},
}
])
我不知道是什么问题,如果有人可以帮助我,我将不胜感激。
【问题讨论】:
标签: node.js mongodb mongoose nosql aggregation-framework