title: How to write a mongoDB query to match any element in an array of subdocuments against any element in a Typescipt Array?
body: <p>I have a mongoDB database containing <strong>Events</strong>. Each event is defined by a number of fields, including an <strong>array of genres</strong> (which are subdocuments consisting of a {genre and subGenre}).</p>
  
  <p>For example: an event could be of {genre: "music", subGenre: "jazz"}, and {genre: "music", subGenre: "blues"}. See <strong>event.js</strong> - the model. In this case, 2 "genre" sub documents are added to the "genres" array in the Event document in the database.</p>
  
  <p>Back in my node application - see <strong>query.ts</strong> - I am trying to work out how to run a query that lets the user search for all events that match their genre preferences.</p>
  
  <p>So:</p>
  
  <ul>
  <li>the Event is defined by an array of genres (in the database), and </li>
  <li>the user's preferences are defined by an array of genres (in the<br>
  application).</li>
  </ul>
  
  <p><strong>I am looking to define a mongoDB query that returns all Events where there is a match of any 1 {genre, subGenre} combination between the 2 arrays.</strong></p>
  
  <p>I looked into <em>$in Query Selector</em> in the mongoDB documentation and suspect it might need to be used... but programmatically, how do I write a query that expands to include all the values in the variable "searchGenres" in <strong>query.ts</strong>?</p>
  
  <p>Thanks a lot in advance for your thoughts.</p>
  
  <hr>
  
  <p><strong>event.js: mongoDB Model for 'Events" defined using Mongoose - snippet:</strong>    </p>
  
  <pre><code>let mongoose    = require('mongoose');
  
  let EventSchema = new mongoose.Schema({
  
  genres: [
            {
              genre:      String,
              subGenre:   String
            }
         ]
  )};
  
  module.exports =  mongoose.model('Event', EventSchema);
  </code></pre>
  
  <p><strong>query.ts:</strong> </p>
  
  <pre><code>import mongoose   = require('mongoose');
  let Event         = require ('../models/event');
  
  class Genre {
      genre:      string;
      subGenre:   string;
  
      constructor (gen: string, sub: string) {
          this.genre      = gen;
          this.subGenre   = sub;
      }
  }
  
  async function runQuery()
  {
      let searchGenres : Array&lt;Genre&gt; = new Array&lt;Genre&gt;();
  
      // populate searchGenres with some data here... e.g.
      const searchGenre1 : Genre = new Genre ('music', 'jazz');
      const searchGenre2 : Genre = new Genre ('music', 'rock');
  
      searchGenres.push(searchGenre1);
      searchGenres.push(searchGenre2);
  
      // Query logic here:
      // Return all events from the database, if the 'genres' array
      // in the document matches any element in the searchGenres array
      // defined above..
  
      const events = await Event.find ({
                                         'genres': {?help? }
  
                                      });
  
  }
  ```
  
  </code></pre>