title: Node.js + MongoDB query not working properly with "$or" object
body: <p>I'm trying to create a route for users to be able to check other user's profiles. However, I want these profiles to be accesed via 2 different urls <code>/profile/nickname</code> and <code>/profile/id</code> so that a profile can be accesed by using either the user's nickname or user id. I tried the following code:</p>
  
  <pre><code>app.get("/profile/:id", function(req, res) {
  
  User.findOne( { $or : [{ "nickname": req.params.id },{ "_id": req.params.id }] }, function(err, user) {
      if(user)
      {
          res.render('users/profile.jade', {
          locals: { 
              currentUser: user, 
              title: user.nickname +"'s Profile",
              jsf:[],
          }
          });
      }
      else
      {
          res.render('404.jade', { 
              status: 404,
              title: 'Page Not Found', 
                  jsf: []  
          });
      }
  });
  });
  </code></pre>
  
  <p>The problem is, it seems like it is only working with the id and not with the nickname, meaning that if I acces <code>/profile/4f4ae474546708b219000005</code> things work, but if I access <code>/profile/mmellad</code> which is the given nickname for that user, I get the 404 page. </p>
  
  <p>There is also one more thing I figured out that works fine for the nicknames, which is changing the query from </p>
  
  <pre><code>User.findOne( { $or : [{ "nickname": req.params.id },{ "_id": req.params.id }] }
  </code></pre>
  
  <p>to</p>
  
  <pre><code>User.findOne( { "nickname": req.params.id } }
  </code></pre>
  
  <p>in this case <code>/profile/mmellado</code> works fine but using the user id obviously doesn't . </p>
  
  <p>What would be the right way to do this? I'm thinking I may be using a wrong approach.</p>
  
  <p>Another thing to mention is that if I try the following code in the mongo console, it works fine as well:</p>
  
  <pre><code>x = db.users.findOne({ $or: [ {nickname:"mmellado"}, {_id:ObjectId("4f4ae474546708b219000005")}  ]})
  </code></pre>
  
  <p>I tested that code by inserting the right nickname and a wrong _id, then tested with a wrong nickname and right _id. In both cases, x ended up containing the object for the record I needed.</p>
  
  <p>I think I may be able to fix it with an additional route, but I'm new to Node.js and Express all together so I'm not sure what the propper approach would be.</p>
  
  <p>Thanks!</p>