如何在猫鼬中嵌套查询?

时间:2020-06-07 07:02:12

标签: node.js mongodb mongoose

这是answerModel的简称。我没有得到预期的结果。我想了解一些如何使用猫鼬执行嵌套查找查询的帮助

"question_id": {
    "topics": [
        "curium"
    ],
    "_id": "5edb42f9a0b1c85754d6ca24",
    "questionText": "What actually is curium",
    "id": "5edb42f9a0b1c85754d6ca24"
}

以及以下架构定义:

const answerSchema = new mongoose.Schema({
    question_id: {
        type: mongoose.Schema.ObjectId,
        ref: 'Questions',
        required: [true,'Answer must belongs to particular question']
    }
})

并且我还使用查询中间件在查找查询之前进行填充

answerSchema.pre(/^find/, function (next) {
    console.log('Populating answer schem')
    this.populate({
        path: 'question_id',
        select: 'questionText topics'
    });
    next();
})

console.log(req.params.topicaName);        // curium
const answers = await answerModel.find({'question_id.topics': req.params.topicaName });
console.log('answers are', answers)   // answers are [] 

并且我将输出输出到控制台:

curium
Populating answer schem
answers are []

0 个答案:

没有答案