【问题标题】:Looking for some help -- Querying MongoDB寻求帮助——查询 MongoDB
【发布时间】:2021-05-02 20:26:33
【问题描述】:

我对 MongoDB 很陌生,我正在尝试查询我导入的一些数据。我昨天和今天试了几个小时,我的理解有了进步,但仍然没有任何结果。我尝试从博客、stackoverflow、youtube 甚至 MongoDB 网站一步一步地跟踪事情,但仍然无法正确地将其转换为我的数据。我希望有人能指出我正确的方向,而不是找人为我编写代码。这是我正在做的菜:

数据片段:

{'_id': ObjectId('608dd1a2abf9c97d8d771c41'),
 'competition': {'area': {'id': 2072, 'name': 'England'},
                 'code': 'PL',
                 'id': 2021,
                 'lastUpdated': '2021-04-17T02:20:14Z',
                 'name': 'Premier League',
                 'plan': 'TIER_ONE'},
 'filters': {},
 'season': {'currentMatchday': 34,
            'endDate': '2021-05-23',
            'id': 619,
            'startDate': '2020-09-12',
            'winner': None},
 'standings': [{'group': None,
                'stage': 'REGULAR_SEASON',
                'table': [{'draw': 5,
                           'form': 'W,W,L,W,W',
                           'goalDifference': 47,
                           'goalsAgainst': 24,
                           'goalsFor': 71,
                           'lost': 4,
                           'playedGames': 34,
                           'points': 80,
                           'position': 1,
                           'team': {'crestUrl': 'https://crests.football-data.org/65.svg',
                                    'id': 65,
                                    'name': 'Manchester City FC'},
                           'won': 25}}

我想要达到的目标: 我试图查询standings.table.team.name 但没有成功。我退后一步,开始专注于查询competition.name。

我尝试过的: 我使用以下查询尝试使用 Python 的一些(不是全部)事情:

db.standings.find_one( {'standings.table.team.name': "Manchester City FC"})
db.standings.find_one( {'standings.table.team.name': 'Manchester City FC'}) 
db.standings.find_one( {'competition.name': "Premier League"})
db.standings.find_one( {"competition.name": 'Premier League'})
db.standings.find_one( {'competition.name': 'Premier League'})
db.standings.find_one( {"competition.name": "Premier League"})

-- 我这里的所有结果都返回数据库中的所有数据。

在 MongoDB Compass UI 中进行的尝试:

  • 手动输入过滤器:{'competition.name': 'Premier League'}

  • 手动输入过滤器:{"competition.name": 'Premier League'}

  • 手动输入过滤器:{'competition.name': "Premier League"}

  • 手动输入过滤器:{'standings.table.team.name': 'Manchester City FC'}

  • 手动输入过滤器:{'standings.table.team.name': "Manchester City FC"}

  • 我转到 Schema 并导航,为英超联赛和曼城足球俱乐部添加过滤器。然后回到文件,仍然没有运气。我要么什么都没有返回,要么数据库中的所有数据都没有。我不能只得到这个字段。

我也在 Mongo Shell 中尝试过,但我上面使用的语法几乎是我在 shell 中不断重复的,但仍然没有运气。

对于我在这里缺少的内容,我可以获得任何帮助或指示吗?我确实尝试过研究并尝试不同的变化和组合,但仍然没有得到任何结果。

【问题讨论】:

    标签: mongodb mongodb-query pymongo-3.x


    【解决方案1】:

    如果我理解正确,您只想接收选定的键/值用于您的查询。你能证实这一点吗?在这种情况下,请尝试参考 findOne 的文档 - 您需要定义投影,即您想要返回的字段,否则您将获得所有字段(如果过滤器不合适,则什么都没有)。

    我想你正在使用 PyMongo。 Please see here for documentation, find_one 似乎使用与find 相同的参数。

    也许您可以尝试db.standings.find_one( {'competition.name': 'Premier League'}, {'competition.name': 1}) 之类的方法 - 这应该返回一个与过滤器匹配并包含competition.name 字段的对象。

    【讨论】:

    • 抱歉耽搁了,这里有点忙。关于您在帖子中的问题,是的,我正在尝试获取键值对并且我正在使用 PyMongo,这是正确的。我对您提供的代码进行了测试,并进行了一些小的修改。我不得不将第二个 {} 中的competition.name 用单引号括起来,这就像一个魅力。我能够以此为基础继续学习和发展更复杂的查询。感谢您的帮助!
    • 很高兴听到。是我的错字,省略了单引号;我刚刚更正了。
    【解决方案2】:

    试试这个:

    db.standings.find({
      "standings.table.team.name": "Manchester City FC"
    })
    

    这是一个有效的 sn-p:https://mongoplayground.net/p/7ao0ZAiZRwK

    【讨论】:

      猜你喜欢
      • 2014-02-24
      • 2011-09-22
      • 1970-01-01
      • 2021-05-27
      • 2011-02-23
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 2010-11-15
      相关资源
      最近更新 更多