title: Update Express session for multiple users, not in the request context
body: <p>I save some data belonging to the authenticated user in the session and I want that when the user object is updated in the database <code>users</code> collection, to set somehow the updated data in the session of the user. This may not happen in the <em>request - response</em> workflow. Maybe it's a random script updating user data in the database.</p>
  
  <p>How to update a specific session data with the new data that is set in the database?</p>
  
  <p>Currently to set the data I do:</p>
  
  <pre><code>req.session.userId = 7;
  req.session.foo = 42;
  </code></pre>
  
  <p>Let's assume that I want to turn <code>42</code> into <code>43</code> for the session data which has <code>userId === 7</code>. How to do that?</p>
  
  <p>I'm using the <a href="https://github.com/expressjs/session" rel="nofollow noreferrer"><code>express-session</code></a> module.</p>
  
  <p>I know that one solution is to reload from the database the user on each request, but that would not be performant, since it would create queries that can be avoided.</p>
  
  <p>What's the best way to update the session data of the user, knowing a field that was set in the session (such as the <code>userId</code>)?</p>
  
  <p>I use the <a href="https://github.com/jdesboeufs/connect-mongo" rel="nofollow noreferrer"><code>connect-mongo</code></a> store.</p>