async function setVectorIndex(collection, indexDefinition) {
  const existingIndexes = await collection.listSearchIndexes().toArray();
  const defaultIndex = existingIndexes.find((index) => index.name === 'default');
  if (!defaultIndex) {
    await collection.createSearchIndex({ name: 'default', definition: indexDefinition });
    console.log(`Created vector search index for ${collection.collectionName} with dimensions: ${indexDefinition.mappings.fields.embedding.dimensions}.`);
  } else if (defaultIndex.latestDefinition?.mappings?.fields?.embedding?.dimensions !== indexDefinition.mappings.fields.embedding.dimensions) {
    await collection.updateSearchIndex('default', indexDefinition);
    console.log(`Updated vector search index for ${collection.collectionName} with dimensions: ${indexDefinition.mappings.fields.embedding.dimensions}.`);
  }
}
Helper function to update or set a vector index in MongoDB Atlas with a new index definition