Mongo creating inserting duplicate documents

Hi, while running this code, at times it appears to insert double insert documents, almost as if there is some race condition. This happens when the get function is triggered right after another. Any ideas?

export const create = async (collectionName, id) => {
logger.debug(‘Settings.create:’, collectionName, id);

const cacheKey = ${collectionName}_${id};
const collection = database.collection(collectionName);
const defaultDocument = defaultDatabaseValues[collectionName] ?? {};
const data = { id, …defaultDocument };

try {
const insertedDocument = await collection.insertOne(data);
logger.debug(‘inserted document:’, insertedDocument);
} catch (error) {
logger.error(‘Settings:’, error);
}

cache.set(cacheKey, data);
return data;
};

export const get = async (collectionName, id, force = false) => {
logger.debug(‘Settings.get:’, collectionName, id, _getCallerFile());

const cacheKey = ${collectionName}_${id};
const collection = database.collection(collectionName);

logger.debug(‘cache size:’, cache.size);

if (force || !cache.has(cacheKey)) {
try {
let document = await collection.findOne({ id });

  logger.debug('document:', document);

  if (document === null) {
    return await create(collectionName, id);
  }

  cache.set(cacheKey, document);
  return document;
} catch (error) {
  logger.error('Settings:', error);
}

}

return cache.get(cacheKey);
};