Mongo DB-查询对象的嵌套数组 [英] Mongo db - Querying a nested array of objects

查看:95
本文介绍了Mongo DB-查询对象的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很努力地用下面的数据构建REST api,在客户端一切正常,但是当我需要发出POST请求来更新 players 时,我不知道该怎么做.查询mongodb.

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.

路线

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

mongodb查询

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);    
            }
        });
    }
};

Json数据

[
   {
      "__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"
         }
      ]
   }
]

推荐答案

当您尝试在玩家数组中搜索时,我认为您将必须使用elemMatch:

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

请找到与此相关的文档: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/

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

PS:我没有尝试过,因为我的系统上现在没有带有Mongo的NodeJS.

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

这篇关于Mongo DB-查询对象的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆