Error Code 48 in MongoDB typically indicates that you are attempting to create a collection that already exists.
This can happen when the application tries to register a Mongoose model multiple times.
I have checked the documentation for Mongoose using this link and used dropCollection
, similar to your approach.
However, from the documentation, there is an official method to drop a collection.
It is advisable to use the mongodb
package to drop the collection. Refer to this GitHub repository for the complete code in JavaScript.
I referred to this document to create, drop, and retrieve a collection in Azure Cosmos DB for MongoDB.
const indexes = await collection.indexes();
console.log(`Indexes on collection:\n${JSON.stringify(indexes, null, 2)}`);
const dropCollectionResult = await db.dropCollection(collectionName);
console.log(`Collection dropped: ${dropCollectionResult}`);
} catch (error) {
console.error("Error connecting to Azure Cosmos DB:", (error as Error).message);
Output: