Search code examples
mongodbmongodb-querypymongo-3.x

Looking for some help -- Querying MongoDB


I am pretty new to MongoDB and I am trying to query some data I have imported. I have tried for several hours yesterday and again today which I have made progress on my understanding, but still have turned up nothing. I tried following things step by step from blogs, stackoverflow, youtube, and even MongoDB website, but still have not been able to translate it correctly to my data. I was hoping someone can point me in the right direction, not looking for someone to write the code for me. Here is what I am cooking with:

Snippet of the data:

{'_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}}

What I am trying to achieve: I was trying to query for standings.table.team.name but that was not successful. I stepped back and started focusing on querying competition.name.

What I have tried: Some (not all) of the things I tried using Python using the following queries:

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

-- All my results here returns all the data in the database.

Attempts made in the MongoDB Compass UI:

  • Manually typed out the filter: {'competition.name': 'Premier League'}

  • Manually typed out the filter: {"competition.name": 'Premier League'}

  • Manually typed out the filter: {'competition.name': "Premier League"}

  • Manually typed out the filter: {'standings.table.team.name': 'Manchester City FC'}

  • Manually typed out the filter: {'standings.table.team.name': "Manchester City FC"}

  • I went over to Schema and navigated through to add the filter for Premier League and Manchester City FC. Then went back to documents and still no luck. I either get nothing returned or all the data in the database. I am not able to get just this field.

I also attempted in the Mongo Shell, but the syntax I used above is pretty much what I kept repeating in the shell but still no luck.

Can I get any help or pointers with what I am missing here? I really did try to research and try different variations and combinations but still not getting anything.


Solution

  • If I understand correctly you want to receive only selected keys/values for your query. Can you confirm this? In this case please try to refer to the documentation of findOne - you would need to define the projection, i.e. the fields that you want returned, otherwise you will get all fields (or nothing if the filter is not adequate).

    I presume you are using PyMongo. Please see here for documentation, find_one seems to use the same parameters as find.

    Maybe you can try something like db.standings.find_one( {'competition.name': 'Premier League'}, {'competition.name': 1}) - this should return one object that matches the filter and contains the competition.name field.