Username does not work for login, only email works

I have a customer mongodb connection. The connection successfully creates users in my mongodb cluster with create user and with require username, however when I attempt to login, I receive a profile not defined error if I sign in with username instead of email. When I use email instead of username the correct user is found.
The only edition I’ve made to login or get user is to add my custom mongo db connection url. I haven’t altered the actual code at all for those scripts?
I asked this question previously and did not figure out a solution,
I am happy to send my account details for someone to take a closer look but please help me get this resolved I’ve been struggling with this,

Here is what the getUser custom function template for mongodb looks like, how should I alter this to get the user based on username?

function getByEmail(email, callback) {
const MongoClient = require(‘mongodb@3.1.4’).MongoClient;
const client = new MongoClient(‘mongodb+srv://mern:’+configuration.MONGO_PASSWORD+’’);

client.connect(function (err) {
if (err) return callback(err);

const db = client.db('test');
const users = db.collection('users');

users.findOne({ email: email }, function (err, user) {

  if (err) return callback(err);
  if (!user) return callback(null, null);

  return callback(null, {
    user_id: user._id.toString(),
    nickname: user.nickname,


This looks to be a duplicate post, please check my response here: Username does not work for custom mongodb connection