Insert or Update into MongoDB using mongoose.js

I am developing a REST API with node.js, express.js, MongoDB and mongoose.js and have found that this works within the callback in app.put('/api/users/:userId',function(req, res) { });

  new User({ userId : requestData.userId}).findByUserId(function (err, users) {
    if (err) {
      response.status = 'Find Error';
      response.error = err;
      debug('ERROR: ', err);
      res.send(response);
    } else if (!users.length) {
      response.status = 'Registered a new user...';
      debug(response.status);
      var newUser = extend(new User({ userId : requestData.userId, created : Date.now() }), requestData);
      newUser.save(function (err, saveduser) {
        if (err) {
          response.status = 'Insert Error';
          response.error = err;
          debug('ERROR: ', err);
        } else {
          response.user = saveduser;
          debug('New user saved: ', saveduser);
        }
        res.send(response);
      });
    } else {
      response.status = 'Found registered user...';
      debug(response.status);
      if (users.length !== 1) {
        response.status = 'Multiple Users error';
        response.error = 'Found more than one user with userId: ' + requestData.userId;
        response.user = users;
        debug('ERROR: Duplicate entries for userId %s. Users are: ', requestData.userId, users);
        res.send(response);
      } else {
        var existingUser = users[0];
        existingUser.updated = Date.now();
        response.user = existingUser;
        existingUser.save(function (err, user) {
          if (err) {
            response.status = 'Insert Error';
            response.error = err;
            debug('ERROR: ', err);
          } else {
            response.user = user;
            debug('User updated: ', user);
          }
          res.send(response);
        });
      }
    }

I have seen the upsert option, but could not really get my head around it.

This definitely does not look like the right approach and I have not been able to find any good article on how to do such a thing in the right way, considering the callback based model and the error argument in it.

Could someone point me in the right direction?

I don't think your notion of a single route that will either update an existing user or create a new one is a good idea. Instead, consider following the REST design:

app.post('/api/users', createNewUser);
app.put('/api/users/:userId', updateUser);

createNewUser should just make a new user object with the proper attributes and call save on it. updateUser can do User.update({_id: req.params.userId}, fieldsToChange.... See if you can make that work to start and don't add complications like checking for already registered users until a simplistic create/update pair works OK and you feel confident. Then you can evolve your code to be more robust and realistic, but go step by step.