如何解决错误检测到未知的 BSON 类型

Posted

技术标签:

【中文标题】如何解决错误检测到未知的 BSON 类型【英文标题】:How to solve error Detected unknown BSON type 【发布时间】:2021-12-26 20:22:43 【问题描述】:

您好,我正在尝试在使用 Mongo 4.4.6 数据库时找出 org.mongodb:mongodb-driver-sync 和 org.springframework.data:spring-data-mongodb 之间的正确依赖关系。仍然得到:

2021-11-15 17:13:18,112 pool-9-thread-1 ERROR An exception occurred processing Appender console org.bson.BsonSerializationException: Detected unknown BSON type "\x73" for fieldname "tBatch". Are you using the latest driver version?
    at org.bson.BsonBinaryReader.readBsonType(BsonBinaryReader.java:95)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:82)
    at com.mongodb.internal.connection.ByteBufBsonDocument.toBsonDocument(ByteBufBsonDocument.java:158)
    at com.mongodb.internal.connection.AbstractByteBufBsonDocument.entrySet(AbstractByteBufBsonDocument.java:100)
    at org.apache.logging.log4j.message.ParameterFormatter.appendMap(ParameterFormatter.java:562)
    at org.apache.logging.log4j.message.ParameterFormatter.appendPotentiallyRecursiveValue(ParameterFormatter.java:498)
    at org.apache.logging.log4j.message.ParameterFormatter.recursiveDeepToString(ParameterFormatter.java:456)
    at org.apache.logging.log4j.message.ParameterFormatter.formatMessage2(ParameterFormatter.java:189)
    at org.apache.logging.log4j.message.ParameterizedMessage.formatTo(ParameterizedMessage.java:228)
    at org.apache.logging.log4j.core.pattern.MessagePatternConverter.format(MessagePatternConverter.java:123)
    at org.apache.logging.log4j.core.pattern.PatternFormatter.format(PatternFormatter.java:38)
    at org.apache.logging.log4j.core.layout.PatternLayout$PatternSerializer.toSerializable(PatternLayout.java:345)
    at org.apache.logging.log4j.core.layout.PatternLayout.toText(PatternLayout.java:244)
    at org.apache.logging.log4j.core.layout.PatternLayout.encode(PatternLayout.java:229)
    at org.apache.logging.log4j.core.layout.PatternLayout.encode(PatternLayout.java:59)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:197)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:190)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:181)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:543)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:502)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:485)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:460)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:82)
    at org.apache.logging.log4j.core.Logger.log(Logger.java:161)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2198)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2152)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2135)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2058)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1939)
    at org.apache.logging.log4j.spi.AbstractLogger.debug(AbstractLogger.java:480)
    at com.mongodb.internal.connection.ProtocolHelper.sendCommandSucceededEvent(ProtocolHelper.java:289)
    at com.mongodb.internal.connection.LoggingCommandEventSender.sendSucceededEvent(LoggingCommandEventSender.java:142)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:306)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:258)
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:500)
    at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71)
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:224)
    at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:110)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:343)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:334)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommandWithConnection(CommandOperationHelper.java:220)
    at com.mongodb.internal.operation.FindOperation$1.call(FindOperation.java:631)
    at com.mongodb.internal.operation.FindOperation$1.call(FindOperation.java:625)
    at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:462)
    at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:625)
    at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:77)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:190)
    at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
    at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2771)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2507)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2489)
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:856)
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:842)

奇怪的是查询成功执行,然后响应被传递给CommandListener,在那里,在记录期间它抛出异常。

有人能指出我正确的方向吗?

【问题讨论】:

BSON 中没有类型 \x73。您可以直接从 mongo shell 运行该查询吗? 是的,正如我所说的查询成功执行,然后传递给CommandListener 当然是这样,如果你在 mongo shell 中运行查询,你会真正看到没有名为 tBatch 的字段。 是的,因为驱动程序损坏,在读取 ByteBufBsonDocument 时将“firstBatch”剪切为“tBatch” 【参考方案1】:

我已通过在 commandStarted 中的 CommandListener 实现中使用 BsonDocument.clone() 来修复它。问题是在执行命令期间,缓冲区位置被修改,所以我们以后不能在commandSucceeded中依赖它

【讨论】:

以上是关于如何解决错误检测到未知的 BSON 类型的主要内容,如果未能解决你的问题,请参考以下文章

如何解决sprintf参数类型错误导致程序崩溃

如何更正值错误:未知标签类型“连续”

如何在 Haskell 中将数据类型转换为 BSON?

如何解决此模拟器:错误:未知皮肤名称“WVGA800”解决方案

如何解决错误“无法从'serial'(未知位置)导入名称'serial'”?

如何解决错误:Laravel-MySql 中的 SQL 身份验证方法未知