title: find whether individual array element exists in the mongodb document's array
body: <p>Is there a way to find whether the individual element exists in the mongodb document's array.
  <br/><br/>For Example:
  If I have a document</p>
  
  <pre><code>{
      "_id": "A",
      "userId": "B",
      "selectedUsers": ["C", "D"]
  }
  </code></pre>
  
  <p>and I have another array as ["E", "C"]. <br/>I want to write a query which gives the result as [false, true] when userId and the above array is supplied to it because "E" is not in the selectedUsers array and "C" is in the selectedUsers array.</p>
  
  <p>I Know I can just first find the document with the given userId and then use Array.find() on it for individual element. But I want to know if there's a way to do this in mongodb query.</p>
  
  <p>Also, I am using nodeJS and mongoose.</p>
  
  <p>Just to be clear I need to get the same functionality as the below js code using mongodb query.</p>
  
  <pre class="lang-js prettyprint-override"><code>// req is the http request
  // Selected is the mongoose Model for above document
  // usersToCheck = ["E", "C"] (this is the second array)
  const result = Selected.findOne({ userId: req.userId});
  const { selectedUsers } = result;
  const response = usersToCheck.map(uid =&gt; {
      if(selectedUsers.some(id =&gt; id === uid)){
          return true;
      }
      return false;
  })
  
  // response = [false, true]
  </code></pre>
  
  <p>Now the above code have a complexity of O(n^2) assuming the size of usersToCheck and selectedUsers is n.</p>
  
  <p>I want to some how get the same response as above using mongodb query. So, that I can index the collection based on the selectedUsers array and improve the time required to get the response.</p>
  
  <p>Or, any other way to improve the above code is appreciated.</p>
  
  <p>Thanks in advance for your help.</p>