Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用聚合匹配mongoose中两个集合中的数据?_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 如何使用聚合匹配mongoose中两个集合中的数据?

Node.js 如何使用聚合匹配mongoose中两个集合中的数据?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个测验应用程序,其中模式如下: 测验模式- const quizSchema = new mongoose.Schema({ userID: { type: String, required: [true, 'User ID required'] } }); 响应模式- const responseSchema = new mongoose.Schema({ userID: { type: String, required: [true, 'U

我有一个测验应用程序,其中模式如下: 测验模式-

const quizSchema = new mongoose.Schema({
  userID: {
    type: String,
    required: [true, 'User ID required']
  }
});
响应模式-

const responseSchema = new mongoose.Schema({
  userID: {
    type: String,
    required: [true, 'User ID required']
  },
  quizID: {
    type: String,
    required: [true, 'Quiz ID required']
  }
});
响应模式具有用户对测验的响应,即如果尝试测验,则只有它存储在响应模式中

我可以很容易地找到用户使用

const attempted = await Response.find({userID});
下面是我如何使用基本逻辑完成的:-

exports.unattemptedQuizDB = async (userID) => {
    try{
        const quizzes = await Quiz.find({});
        const filtered = [];
        for (const quiz of quizzes){
            const inResponse = await Response.find({userID:userID, quizID: quiz._id});
            if (inResponse.length === 0){
                filtered.push(quiz._id);
            }
        }
        return filtered;
    }catch(e){
        throw new Error(e);
    }
};

但是我如何使用聚合找到没有吸引力的测验呢?

下面是您编写的逻辑的聚合查询:

let responses=wait quick.aggregate([
{
$lookup:{
来自:“回复”,
让:{
userID:“$userID”,
quizID:{$toString:“$\u id”}
},
管道:[
{
$match:{
$expr:{
美元及:[
{$eq:[“$userID”,“$$userID”]},
{$eq:[“$quizID”,“$$quizID”]}
]
}
}
}
],
as:“回应”
}
},
{
$match:{
“答复”:{$eq:[]}
}
}
]);

但未受诱惑的测验在
回答中不会有任何条目
收集权限?还有
quizID
responses
中是否存在于
quizzes
集合中?是的,这就是我想要得到的,如果用户的quizID不在responsecollection中,它是不具诱惑力的。idstring?我觉得很难理解,虽然我知道如果用户ID不在响应集合中,上面的代码会返回查询,对吗?检查更新的答案。它将从
测验
集合中获取用户在
响应
集合中未尝试的所有文档。