title: How to get all the values that contains part of a string using mongoose find?
body: <p>I have the following problem retrieving data from MongoDB using mongoose.</p>
  
  <p>Here is my Schema:</p>
  
  <pre><code>const BookSchema = new Schema(
      {
          _id:Number,
          title:String,
          authors:[String],
          subjects:[String]   
      }
  );
  </code></pre>
  
  <p>as you can see i have 2 arrays embedded in the object, let's say the content of the authors can be something like this: authors:["Alex Ferguson", "Didier Drogba", "Cristiano Ronaldo", "Alex"]<br>
  what I'm trying to achieve is get all the Alex in the array.</p>
  
  <p>So far, I've been able to get the values if they match the value completely. However if I try to get the ones containing Alex the answer is always [].</p>
  
  <p>What I want to know is how I can do this using find() without performing a map-reduce to create a view or a collection and then applying find() over that.</p>
  
  <p>The code here works for exact matches</p>
  
  <pre><code>Book.find( {authors:req.query.q} , function(errs, books){
              if(errs){
                  res.send(errs); 
              }
  
              res.json(books);
          });
  </code></pre>
  
  <p>I tried some things but no luck 
  {authors:{$elemMatch:req.query.q}}
  {authors:{$in:[req.query.q]}}</p>
  
  <p>This one gives me an error and on top of that says is very inefficient in another post I found here.
  {$where:this.authors.indexOf(req.query.q) != -1}</p>
  
  <p>and I also tried {authors:{$regex:"./<em>value</em>/i"}}</p>
  
  <p>The map-reduce works fine, I need to make it work using the other approach to see which one is better?  </p>
  
  <p>Any help is greatly appreciated. I'm sure this is easy, but I'm new with NodeJS and Mongo and I haven't been able to figure it out on my own.</p>