com.mongodb.MongoCommandException: Command failed with error 48 (NamespaceExists) MongoDB

created at 08-18-2021 views: 42

error message

com.mongodb.MongoCommandException: Command failed with error 48 (NamespaceExists): 'a collection 'mapMongoDB.macCode01' already exists' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "a collection ' **mapMongoDB.macCode01** ' already exists", "code" : 48, "codeName" : "NamespaceExists" }
    at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:293)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
    at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
    at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:170)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:163)
    at com.mongodb.operation.CreateCollectionOperation$1.call(CreateCollectionOperation.java:381)
    at com.mongodb.operation.CreateCollectionOperation$1.call(CreateCollectionOperation.java:377)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:453)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:415)
    at com.mongodb.operation.CreateCollectionOperation.execute(CreateCollectionOperation.java:377)
    at com.mongodb.operation.CreateCollectionOperation.execute(CreateCollectionOperation.java:54)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:193)
    at com.mongodb.client.internal.MongoDatabaseImpl.executeCreateCollection(MongoDatabaseImpl.java:300)
    at com.mongodb.client.internal.MongoDatabaseImpl.createCollection(MongoDatabaseImpl.java:257)
    at com.mongodb.client.internal.MongoDatabaseImpl.createCollection(MongoDatabaseImpl.java:252)
    at com.example.demo.Demo.mongoDBRepository.mongoDBRepository.main(mongoDBRepository.java:24)

reason

The collection in the current library already exists, and an exception will be thrown if it is created again
Solution 1
Delete the existing collection:

db.macCode01.drop();

Solution 2
Create a new collection:

db.createCollection("collName", {size: 20, capped: 5, max: 100});//Created successfully will display {"ok":1}
created at:08-18-2021
edited at: 08-18-2021: