title: Returning only one item of an array in mongoose?
body: <p>I am trying to grab one array element and update a bunch of it's properties. Here is how I am currently doing it which feels like the wrong way to me. Can this be done without having to loop through the array to match the _id? Can you select just one of the array elements by _id to be updated?</p>
  
  <pre><code>Schemas.Users.findOne({ _id: req.session.userID })
  .select('friends').exec(function(err, user){
    if (err) return next(err);
  
    // figure out which friend I should be updating
    var index = null;
    for (var i = 0; i &lt; user.friends.length; i++) {
      if (user.friends[i]._id == req.params.friendID) {
        index = i;
        break;
      }
    }
  
    // if we didn't find the friend, something went wrong
    if (index === null) {
      console.log('Error: index not found.');
      return res.redirect('somepage');                
    }
  
    // update friend
    user.friends[index].name = req.body.name;
  
    user.markModified('friends');
    user.save();
  
    return res.redirect('somepage');
  });
  </code></pre>