title: How do I search one array for items from another array in MongoDB?
body: <p>I have a <code>users</code> table that has the following data structure:</p>
  
  <pre><code>[{
      userID: 0,
      username: 'test0',
      petsForSale: [
          { type: 'fish' },
          { type: 'cats' }
      ],
      seekingPets: [
          { type: 'dogs' },
          { type: 'birds' }
      ]
  },
  {
      userID: 1,
      username: 'test1',
      petsForSale: [
          { type: 'guinea pigs' },
          { type: 'dogs' },
          { type: 'hamsters' }
      ],
      seekingPets: [
          { type: 'ferrets' }
      ]
  }]
  </code></pre>
  
  <p>I'm trying to execute a <code>GET</code> that returns matched users based on <code>petsForSale</code> and <code>seekingPets</code>. For example, if a user is selling <code>dogs</code>, they will show up on the list of matched results for any user with <code>dogs</code> in <code>seekingPets</code>. I'm very close, here's my router code so far:</p>
  
  <pre><code>router.get('/:id/findmatches', function(req, res) {
    var db = req.db;
    var collection = db.get('users');
    var uid = req.params.id;
  
    //var seeking_pets = collection.find({ userID: uid }, { seekingPets: 1 });
    //var seeking_pets = collection.find({ userID: uid }, { seekingPets: { type: 1 }});
    var seeking_pets = [ 'dogs', 'birds' ]; // *Hard-coded is the only way I can get it to work
  
    collection.find({ petsForSale: { $elemMatch: { type: { $in: seeking_pets }}}}, function(e, docs) {
        res.json(docs);
    });
  });
  </code></pre>
  
  <p>This code compiles and works just fine with <code>seeking_pets</code> hard-coded - visiting <code>/users/0/findmatches</code> returns user <code>test1</code> as expected. I'm stuck extracting the list of <code>seekingPets</code> from the <code>userID</code> in the request and searching through it in <code>collection.find</code>. The two commented lines are what I've tried without success. I've also tried converting the <code>collection</code> to an <code>array</code>.</p>