0

Using the 3 documents as an example:

{ _id: '/players/b/berrijo01.shtml',
  url: '/players/b/berrijo01.shtml',
  name: 'Jose Berrios',
  image: 'https://www.baseball-reference.com/req/202108020/images/headshots/d/d94db113_mlbam.jpg',
  teams: 
   [ { name: 'MIN', years: [ 2016, 2017, 2018, 2019, 2020, 2021 ] },
     { name: 'TOR', years: [ 2021 ] } ] }
{ _id: '/players/c/cruzne02.shtml',
  url: '/players/c/cruzne02.shtml',
  name: 'Nelson Cruz',
  image: 'https://www.baseball-reference.com/req/202108020/images/headshots/f/fea2f131_mlbam.jpg',
  teams: 
   [ { name: 'MIL', years: [ 2005 ] },
     { name: 'TEX',
       years: [ 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 ] },
     { name: 'BAL', years: [ 2014 ] },
     { name: 'SEA', years: [ 2015, 2016, 2017, 2018 ] },
     { name: 'MIN', years: [ 2019, 2020, 2021 ] },
     { name: 'TBR', years: [ 2021 ] } ] }
{ _id: '/players/m/mauerjo01.shtml',
  url: '/players/m/mauerjo01.shtml',
  name: 'Joe Mauer',
  image: 'https://www.baseball-reference.com/req/202108020/images/headshots/4/43c69595_mlbam.jpg',
  teams: 
   [ { name: 'MIN',
       years: 
        [ 2004, 2005, 2006, 2007,
          2008, 2009, 2010, 2011,
          2012, 2013, 2014, 2015,
          2016, 2017, 2018 ] } ] 
}

I want to get any document that has the specified year within the teams array inside the years value.

So for example if I query 2021 I would get the first and second documents. If I query for 2006 I get the second and third documents. I'm not sure how to go about this query as I'm fairly new to mongo.

1 Answer 1

1

its very simple and you can find details here https://docs.mongodb.com/manual/tutorial/query-arrays/ , use below filter:

db.collection.find({
  "teams.years": 2021
})

and

db.collection.find({
  "teams.years": 2006
})

pls refer here https://mongoplayground.net/p/S87eJKZCjwd

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.