title: Mongoose regex with populated document field
body: <p>I would like to improve this request, I need to obtain all the followed users that match the query, the problem is that the &quot;followed user&quot; model contains a user reference, so for the request I must establish a match between the query and the name of populated user, but when I try to do it with dot notation it doesn't work: 'followedUser.username': {$ regex: query, $ options: i}, so I tentatively decided to generate the query using &quot;match&quot; in populate, but that generates problem and it is that it returns all the follow-ups with followUser null and those that match it returns followUser with the user data. This way it works but it doesn't seem like a good solution to me. Can someone help me improve it.</p>
  <p>This way no works:</p>
  <pre><code>const options = 
      {
          offset: offset||0,
          limit:limit|| 80,
          sort:{username : - 1},
          collation: {
            locale: 'en',
          },
          select:('followedUser'),
          populate:[{path:'followedUser',select:['id','name','username','avatar']}] 
      }
      const filter = 
      {
          $and:
          [
              {$or: 
              [
                  {
                    'followedUser.name': 
                    {
                      $regex: query,
                      $options: 'i',
                    },
                  },
                  {
                    'followedUser.username': 
                    {
                      $regex: query,
                      $options: 'i',
                    },
                  },
              ]},
              {user:id}
          ]
      }
      const users = await FollowUser.paginate(filter,options)
  </code></pre>
  <p>This Works:</p>
  <pre><code>async function searchFollowedUsers(query,offset,limit,context)
  {
      //se debe mejorar esta peticion
      const{id}=context.user
    
      const users = await FollowUser.find({user:id}).populate([{path:'followedUser',match:{username:{$regex:query,$options:'i'}}}])
      try 
      {
          const filter = users.filter(follow=&gt;follow.followedUser!=null)
          if(filter.length &lt;= 0)
          {
              throw new Error('No existen usuarios')
          }
          else
          {
              return filter 
          }
      } 
      catch (error) 
      {
          throw new Error(error.message)
      }
  }
  </code></pre>