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

2021/7/20 19:40:15

本文主要是介绍com.mongodb.MongoCommandException: Command failed with error 48 (NamespaceExists) MongoDB报错,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  1. 报错
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)

  1. 原因
  • 当前库中的集合已经存在了,再次创建会抛出异常
  1. 解决方案一
  • 删除存在的集合:** db.macCode01.drop(); **
  1. 解决方案二
  • 创建新的集合:** db.createCollection("collName", {size: 20, capped: 5, max: 100});//创建成功会显示{"ok":1} **


这篇关于com.mongodb.MongoCommandException: Command failed with error 48 (NamespaceExists) MongoDB报错的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程