0

I want to get JSON data of a particular lets say match=2 data from all matches. I am not able to get required data which is stored in MongoDB.

JSON response:

{
    "_id": "5a63051735aaddd30d1d89cc",
    "id": 1,
    "season": 2008,
    "city": "Bangalore",
    "team1": "Kolkata Knight Riders",
    "team2": "Royal Challengers Bangalore",
    "toss_winner": "Royal Challengers Bangalore",
    "toss_decision": "field",
    "result": "normal",
    "dl_applied": 0,
    "winner": "Kolkata Knight Riders",
    "win_by_runs": 140,
    "win_by_wickets": 0,
    "player_of_match": "BB McCullum",
    "venue": "M Chinnaswamy Stadium",
    "umpire1": "Asad Rauf",
    "umpire2": "RE Koertzen",
    "umpire3": ""
  }

I am able to get all JSON data of 577 total matches using below code.

app.get('/api/matches', (req, res) =>{

    matches.find({}).then(eachOne => {

        res.json(eachOne);

    });

});

But now I do not want to get JSON data of all matches instead I want to get JSON data of match having id:2 how can I get that I tried using below code but it gets JSON data of all matches.

app.get('/api/matches/:match_id', (req, res) =>{

    let match = req.params.id;

    matches.find({match}).then(eachOne =>{

        res.json(match);
    });

}); 

In matches.js:

const mongoose = require('mongoose');

let Schema = mongoose.Schema;

const matchSchema = new Schema({

    match_id:{
        type:Number,
        required:true
    },

    season:{
        type:Number,
        required:true
    },

    city:{
        type:String,
        required:true
    },

    date:{
        type:Number
    },

    team1:{
        type:String,
        required:true
    },

    team2:{
        type:String,
        required:true
    },


    toss_winner:{
        type:String,
        required:true
    },

    toss_decision:{
        type:String,
        required:true
    },

    dl_applied:{
        type:Number
    },

    winner:{
        type:String,
        required:true
    },

    win_by_runs:{
        type:Number,
        required:true
    },

    win_by_wickets:{
        type:Number,
        required:true
    },

    player_of_match:{
        type:String,
        required:true
    },

    venue:{
        type:String,
        required:true
    },

    umpire1:{
        type:String,
        required:true
    },

    umpire2:{
        type:String,
        required:true
    },

    umpire3:{
        type:String
    }

});

const matches = mongoose.model('matches', matchSchema);

module.exports = matches;

2 Answers 2

1

Your code is almost right. You just need to specify the right query:

app.get('/api/matches/:match_id', (req, res) =>{
    let match = req.params.match_id;
    matches.findOne({id: parseInt(match)}).then(m =>{
        res.json(m);
    });
});

Also consider that match_id from url is a string, so you need to check it and convert to number (I used parseInt)

Sign up to request clarification or add additional context in comments.

20 Comments

Why parseInt(match) it is already I number see matches.js ?
Your code works even if I use match_id: match instead of match_id: ParseInt(match)
Ah! Yes! I forgot that you are using mongoose...it convert it in number for you.
I am new to mongodb so does mongoose convert string into number by default.
Mongoose does it
|
0

You must be using bodyparser by default .

You are doing wrong.

app.get('/api/matches/:match_id', (req, res) =>{



    // instead of req.params.id write req.params.match_id
   let match = req.params.match_id;

    // when you write {match} there is nothing {id : match}

    matches.find({match_id : match}).then(eachOne =>{

        res.json(eachOne);
    });

}); 

13 Comments

My matches.js i.e matches schema is like this imgur.com/a/PFeas
The field in JSON response is id but in matches.js Schema I have named it as match_id in mongodb is it allowed to name them different or do I need to use same name for fields.
add full image or file , it is half image. or add the match.js in question.
check it out I have added matches.js
In JSON response I have just added only 1 JSON object but it gives around 577 objects
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.