0

I am really struggling with building a REST api with the data I have below, everything works fine on the client side but when I need to make POST requests to update players I cannot figure out how to query mongodb.

The route

router.get('/api/teams/:teamid/player/:playerid', player.getById);

The mongodb query

module.exports = {
    getById: function(req, res) {
        models.Team.findOne({"players.player_name":"Jokim"}, function(err, player) {
            if (err) {
                res.json({error: 'player not found.'});
            } else {
                console.log(player);
                res.json(player);    
            }
        });
    }
};

The Json data

[
   {
      "__v":0,
      "_id":"5362dcf7e99615aa392d7d72",
      "assists":80,
      "blocks":14,
      "feed":null,
      "fouls":20,
      "made_one":10,
      "made_three":5,
      "made_two":15,
      "missed_one":4,
      "missed_three":4,
      "missed_two":20,
      "percentage":50,
      "points":44,
      "rebounds":100,
      "steals":33,
      "team_name":"Bulls",
      "players":[
         {
            "player_name":"Jokim",
            "_id":"5365f079ed4914600d9342c7",
            "team_name":"",
            "team_id":"",
            "points":0,
            "made_one":0,
            "made_two":0,
            "made_three":0,
            "missed_one":0,
            "missed_two":0,
            "missed_three":0,
            "percentage":0,
            "assists":0,
            "rebounds":0,
            "steals":0,
            "blocks":0,
            "fouls":0,
            "feed":""
         },
         {
            "player_name":"Taj",
            "_id":"5365f079ed4914600d9342c6",
            "team_name":"",
            "team_id":"",
            "points":0,
            "made_one":0,
            "made_two":0,
            "made_three":0,
            "missed_one":0,
            "missed_two":0,
            "missed_three":0,
            "percentage":0,
            "assists":0,
            "rebounds":0,
            "steals":0,
            "blocks":0,
            "fouls":0,
            "feed":""
         }
      ]
   },
   {
      "team_name":"Wizards",
      "points":44,
      "made_one":10,
      "made_two":15,
      "made_three":5,
      "missed_one":4,
      "missed_two":20,
      "missed_three":4,
      "percentage":50,
      "assists":80,
      "rebounds":100,
      "steals":33,
      "blocks":14,
      "fouls":20,
      "feed":null,
      "_id":"5362dcf7e99615aa392d7d75",
      "__v":0,
      "players":[
         {
            "player_name":"John Wall",
            "points":22,
            "made_one":3,
            "made_two":4,
            "made_three":5,
            "missed_one":2,
            "missed_two":3,
            "missed_three":4,
            "percentage":5,
            "assists":2,
            "rebounds":2,
            "steals":2,
            "blocks":5,
            "fouls":3,
            "feed":null,
            "facebook_id":null,
            "_id":"5362dcf7e99615aa392d7d77"
         },
         {
            "player_name":"Bradley Beal",
            "points":22,
            "made_one":3,
            "made_two":4,
            "made_three":5,
            "missed_one":2,
            "missed_two":3,
            "missed_three":4,
            "percentage":5,
            "assists":2,
            "rebounds":2,
            "steals":2,
            "blocks":5,
            "fouls":3,
            "feed":null,
            "facebook_id":null,
            "_id":"5362dcf7e99615aa392d7d76"
         }
      ]
   }
]
2
  • What is the problem? Any error? Commented May 5, 2014 at 9:58
  • Can you be more specific? What action do you want to do? I suppose you want to create a new 'player' subdocument since you are creating a rest api. Commented May 5, 2014 at 10:01

2 Answers 2

2

As you are trying to search within players array I think you will have to use elemMatch:

Please find the docs related to the same: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/

PS: I have not tried this as I do not have NodeJS with Mongo on my system now.

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

6 Comments

Yeah that sounds right.. I fiddled with many different ways I came across that, but it didnt do me any good I didnt use it properly most likely.. Let me read on that more, I 1+ because I think this is on the right track..
Here is the query { _id: "5362dcf7e99615aa392d7d72" }, { players: { $elemMatch: { player_name: "Jokim" } } } worked like a charm :)
the only problem is the json is not pure, meaning its not the straight up object {"_id":"5362dcf7e99615aa392d7d72","players":[{"player_name":"Jokim","_id":"5365f079ed4914600d9342c7","team_name":"","team_id":"","points":0,"made_one":0,"made_two":0,"made_three":0,"missed_one":0,"missed_two":0,"missed_three":0,"percentage":0,"assists":0,"rebounds":0,"steals":0,"blocks":0,"fouls":0,"feed":""}]}
Seems like this is a good solution for that res.json(player[0].players)
Yeah could be but what happens if there are no records returned. It might throw an error as player[0] will not be present.
|
-2

Usually people are using PUT method to update model.
Check the update method in mongodb docs.

Route

router.put('/api/teams/:teamid/player/:playerid', player.updatePlayer);

Controller

module.exports = {
  updatePlayer: function(req, res) {
    models.Team.update({"players.player_name":"Jokim"}, {$set: {assist: 100}}, function(err, player) {
      if (err) {
        res.json({error: err});
      } else {
        console.log(player);
        res.json(player);    
      }
    });
  }
};

1 Comment

When testing I dont want to have to run a POST every time I want to get a response

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.