Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Mongodb-How to pass collection field as a variable to get data from another collection?

I'm trying to get study materials related to a subject from 'study material' collection when subject name from 'subject' collection is passed.

router.get('/materials',(req,res) =>{
    
    Materials.find({"subjectName":"Physics"}).exec((err,materials) =>{
        if(err){
            return res.status(400).json({
                error:err
            });
        }
        return res.status(200).json({
            success:true,
            existingMaterials:materials
        });
    });
});

This is what I have got so far.How to pass subject name as a variable?

I did the react part this way. Is this correct?

export default class ViewSubjectMaterial extends Component {

    constructor(props){
        super(props);
        
        this.state={
          materials:[]
        };
      }
    
      componentDidMount(){
        this.retrieveMaterials();
      }
      
      retrieveMaterials(){
        axios.get("http://localhost:8000/materials?subjectName=Physics").then(res =>{
          if(res.data.success){
            this.setState({
              materials:res.data.existingMaterials
            });
      
            console.log(this.state.materials)
          }
        })
      }}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Do you need something like this?

router.get('/materials',(req,res) =>{
const subjectName = req.query.subjectName;


Materials.find({"subjectName":subjectName}).exec((err,materials) =>{
    if(err){
        return res.status(400).json({
            error:err
        });
    }
    return res.status(200).json({
        success:true,
        existingMaterials:materials
    });
});
});

Then, you will have to send this in the request query. for example:

GET http://localhost:8000/materials?subjectName=Physics

about 4 years ago · Juan Pablo Isaza Report

0

change get to post method

router.post('/materials',(req,res) =>{

/// {subjectName:"Physics",curriculum:"Intro"} example of req.body
Materials.find(req.body).exec((err,materials) =>{
    if(err){
        return res.status(400).json({
            error:err
        });
    }
    return res.status(200).json({
        success:true,
        existingMaterials:materials
    });
});
});

add these lines in index js where express defined

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!