MongoDB doesn’t have an API/command to update multiple collections at the same time. But you could have a list of collection names and call the update command on each:

const collectionNames = ['users', 'products', 'orders'];
// or
const collectionNames = db.getCollectionNames();

for (const collectionName of collectionNames) {
  updateResult = await db.getCollection(collectionName).updateOne(
    { productName: 'Test' },  // filterCriteria
    { $set: { productName: 'Final' } }  // updateOperation
  );
}

If you have this scnario happening frequently for different fields, put that into a function which accepts a filter and update operation and does the collection loop:

async function updateAllCollections(filterCriteria, updateOperation) {
  // put that loop here
}

await updateAllCollections({ productName: 'Test' }, { $set: { productName: 'Final' } });
await updateAllCollections({ _id: 123 }, { $set: { status: 'Ready' } });
1 Like