title: Keep track of last login date with NodeJS and MongoDB
body: <p>I'm working on adding a last login functionality to my Node app and can't seem to get it to work.  Here's what I've got for a mongoose <code>user</code> schema:</p>
  
  <pre><code>userSchema = new mongoose.Schema({
      username: {
          type: String,
          unique: true
      },
      password: String,
      email: {
          type: String,
          unique: true 
      },
      avatar: String,
      firstName: String,
      lastName: String,
      lastLogin: {
          type: Date,
          default: Date.now
      },
      resetPasswordToken: String,
      resetPasswordExpires: Date,
      isAdmin: {
          type: Boolean,
          default: false
      }
  });
  
  
  userSchema.plugin(passportLocalMongoose);
  
  userSchema.statics.newLogin = function login(id, callback) {
      return this.findByIdAndUpdate(id,{'$set' : { 'lastLogin' : Date.now()} }, { new : true }, callback);
   };
  
  
  module.exports = mongoose.model("User", userSchema);
  </code></pre>
  
  <p>Here's my login route:</p>
  
  <pre><code>router.post("/login", passport.authenticate("local", 
      {
          failureRedirect: "/login"
      }), function(req, res) {
      User.findOneAndUpdate(req.username, {lastLogin: Date.now()}, (err, data) =&gt; {
      if(err) console.log(err);
      else console.log("Successfully updated the lastLogin", data);
  
      res.redirect("/players");
    });
  });
  </code></pre>
  
  <p>I've been able to get the initial date to stick when the account is created; however, when I login with that same account the date remains unchanged.  What am I missing here?</p>
  
  <p>There are a few other questions with similar topics, but none seem to resolve my issue.  Specifically, <a href="https://stackoverflow.com/questions/59859512/how-to-add-last-login-date-in-application-using-nodejs-and-mongodb">this</a> question where I've implemented part of the solution to no avail.  Thanks in advance for the help!</p>
  
  <p>Here's the code currently being tested, example req.body:</p>
  
  <pre><code>{ username: 'anyUserHere', password: 'anyPasswordHere' }
  </code></pre>
  
  <p>req.user:</p>
  
  <pre><code>{
    isAdmin: true,
    _id: 5e9b301a6bb78973c9ec8fae,
    username: 'anyUserHere',
    salt: 'saltValue',
    hash: 'hashValue',
    __v: 0,
    avatar: '../images/admin.jpg',
    email: 'example@example.com',
    firstName: 'first',
    lastName: 'last',
    password: 'anyPasswordHere',
    lastLogin: 2020-05-22T18:35:50.941Z
  }
  </code></pre>
  
  <p>So in this case, the 'anyUserHere' example should be the one being updated, but the update occurs to the first user in Mongo.  Console output:</p>
  
  <pre><code>Successfully updated the lastLogin {
    isAdmin: false,
    _id: 5e939f988ced3e0428c8b521,
    username: 'test',
    __v: 0,
    lastLogin: 2020-05-22T18:38:59.836Z
  }
  </code></pre>