Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Match fields from 2 different collection and display result based on the match mongodb nodejs

I have 2 collection one is user and one is questions. both has common field type. If user type matches with type in questions then the result will be displayed.

const questionsSchema = mongoose.Schema({
    question: { type: String },
    number: { type: Number },
    usertype: {
        type: String,
        enum: ['A' , 'B'],
    },
},{ timestamps: true });

const usersSchema = mongoose.Schema({
    name: { type: String },   
    type: {
        type: String,
        enum: ['A', 'B']
    },
}, { timestamps: true }
);

I want to make a query to fetch questions based on user type. If user type matches then the desired set of questions will be displayed.

This is my question.js file

    export const fetchQuestions = async (req, res) => {
      try {
        const user = await db.findOne('USER', { _id: req.userId });
        
        const type = user.type;
        console.log(type + "user type");
        let answerdQuestions = [];
        let nextQuestions;
        let quiz;
        if (user.quiz) {
          quiz = await db.findOne('QUIZ', { user: req.userId });
          answerdQuestions = quiz.responses.map(response => response.number)
        }
        nextQuestions = getNextQuestions(answerdQuestions);
 // Here I have place query to fetch usertype questions.
        const questions = await db.find('QUESTION', { number: { $in: nextQuestions } });
        res.status(200).json({ questions, quiz: { id: quiz?._id, responded: answerdQuestions.length }, options: options });
        return answerdQuestions;
      } catch (err) {
        console.log(err)
        res.status(500).json({ message: "Something went wrong" });
      }
    };

 

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You just have to fetch questions by usertype. So, use this query:

// Here I have place query to fetch usertype questions.
const type = user.type;
const questionsByUserType = await db.find('QUESTION', { usertype: type });

Just find all the questions from questionsSchema where usertype property matches type property which was derived from usersSchema.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda