title: Mongoose elemMatch returns an empty array
body: <p>I am currently building a web application and I have stumbled upon a problem I wasn't able to solve for the past 12 hours.</p>
  
  <p>I am working on a basic get method where I am trying to retrieve certain work positions (i.e. cashier, clerk) that an employee is working as (the client-side works perfectly).</p>
  
  <p>The mongoose model for WorkPosition is such (models/work-position.js):</p>
  
  <pre><code>var mongoose = require('mongoose');
  var User = require('./user');
  var Schema = mongoose.Schema;
  var schema = new Schema ({
      workplace : {type: String, required: true},
      type : {type: String, required: true},
      status : {type: String, required: true},
      color : {type: String, required: true},
      employees: [{type: Schema.Types.ObjectId, ref:'User'}]
   });
  
  module.exports = mongoose.model('WorkPosition', schema);
  </code></pre>
  
  <p>My get method (routes/work-position.js):</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var jwt = require('jsonwebtoken');
  var User = require('../models/user');
  var mongoose = require('mongoose');
  
  var WorkPosition = require('../models/work-position');
  
  router.get('/:userId', function(req, res, next) {
      const userId = req.params.userId;
      WorkPosition.find({employees: {$elemMatch : {$eq: userId}}})
          .exec(function(err, workPositions) {
              if(err) {
                  return res.status(500).json({
                      title: 'an error has occurred',
                      error: err
                  });
              }
              console.log(userId);
              console.log(workPositions);
              res.status(200).json({
                  message: 'Success',
                  obj: workPositions
              });
          });
  });
  </code></pre>
  
  <p>The problem arises when I try to use the $elemMatch method. The code above, when the WorkPosition.find line is changed to</p>
  
  <pre><code>WorkPosition.find()
  </code></pre>
  
  <p>without any conditions (</p>
  
  <pre><code>{employees: {$elemMatch : {$eq: userId}}}
  </code></pre>
  
  <p>) inside, I am successfully able to retrieve the WorkPosition document that I desire.</p>
  
  <p>However, I want to only retrieve the WorkPosition documents where the 'employees' field in WorkPosition matches the 'userId' I have received from req.params. Therefore, I searched through the mongodb/mongoose API (<a href="http://mongoosejs.com/docs/api.html#query_Query-elemMatch" rel="nofollow noreferrer">http://mongoosejs.com/docs/api.html#query_Query-elemMatch</a>)</p>
  
  <p>where I found the $elemMatch method.</p>
  
  <p>In the mongodb shell, when I do</p>
  
  <pre><code>db.workpositions.find({"employees": { $elemMatch : {$eq: "596823efbac11d1978ba2ee9"}}})
  </code></pre>
  
  <p>where "5968...." is the userId, I am successfully able to query the WorkPosition document.</p>
  
  <p>Through this command, I am able to verify that my logic is correct, and using the mongodb native shell command gets me the document I desire.</p>
  
  <p>However, when I try to convert the same logic to the Mongoose API, which is:</p>
  
  <pre><code>WorkPosition.find().elemMatch('employees', {$eq : userId})
  </code></pre>
  
  <p>I get an empty array, and adding lines</p>
  
  <pre><code>mongoose.set('debug', function (coll, method, query, doc) {
      console.log(coll + " " + method + " " + JSON.stringify(query) + " " + JSON.stringify(doc));
  });
  </code></pre>
  
  <p>in /app.js , I am able to see what the mongoose query translates to native mongodb command which is :</p>
  
  <pre><code>workpositions find {"employees":{"$elemMatch":{"$eq":"596823efbac11d1978ba2ee9"}}} {"fields":{}}
  </code></pre>
  
  <p>. The collection (workpositions), method (find), array to seek (employees) and everything is correctly translated to native mongodb command EXCEPT </p>
  
  <pre><code>"$eq"
  </code></pre>
  
  <p>. The only difference between the shell command that successfully works and the mongoose command in my code is the additional quotation marks around '$eq'.</p>
  
  <p>shell:</p>
  
  <pre><code>db.workpositions.find({"employees": { $elemMatch : {$eq: "596823efbac11d1978ba2ee9"}}})
  </code></pre>
  
  <p>mongoose:</p>
  
  <pre><code>db.workpositions.find({"employees": { $elemMatch : {"$eq": "596823efbac11d1978ba2ee9"}}})
  </code></pre>
  
  <p>I cannot seem to find a way to get rid of these extra quotation marks (which I believe is the cause of the problem). I tried using the native command with mongoose like : </p>
  
  <pre><code>mongoose.connection.db.workpositions.find(.....)
  </code></pre>
  
  <p>but this also results in an error.</p>
  
  <p>What is the proper way to use elemMatch through mongoose? Can anyone enlighten me how to use it? I tried every way I could think of, and I cannot seem to get past this problem.</p>
  
  <p>I am thinking of changing the WorkPosition.employees field so that it holds actual Users (not only userIds), and I can parse through with additional information that exactly matches the Mongoose elemMatch API. However, I believe this will waste a HUGE amount of database space because each WorkPosition has to carry an array of Users.</p>
  
  <p>To have the correct relationship between Users and WorkPositions, the elemMatch is a requirement in my project. I would greatly appreciate any help!</p>