当前位置: 首页 > 知识库问答 >
问题:

spring boot:ReactiveMongo未能将排序规则信息添加到索引规范以创建索引(@索引)

仉宸
2023-03-14

我正在使用Spring Boot webflow和Spring data reactive mongob。我有一个用@Document注释的用户类,它实现了User细节。问题是我有一个带有@Indux(唯一=true)的username字段。当应用程序启动时,即使在数据库上创建索引,它也会抛出下面的错误。

以下是课程:


public abstract class GenericEntity<Tid> {
    
    abstract public Tid getId();
    
}
@Data
@Document(collection = "users")
@FieldDefaults(level = AccessLevel.PROTECTED)
@NoArgsConstructor
@AllArgsConstructor
public abstract class User extends GenericEntity<String> implements UserDetails {


    @Id
    String id;

    @Size(max = 63)
    @NotBlank(message = "First name is missing")
    String firstName;

    @Size(max = 63)
    @NotBlank(message = "Last name is missing")
    String lastName;

    @NotBlank
    @Size(min = 5, max = 40)
    @Email(message = "Invalid email address")
    @Indexed(unique = true)
    String username;

    @NotBlank
    @Size(min = 8, max = 255)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    String password;

    @NotNull
    Boolean enabled = true;

    @NotEmpty
    List<Role> roles;
}
@Data
@Document(collection = "users")
@TypeAlias(value = "com.abcd.photograph.Photograph")
public class Photograph extends User {

    @NotNull
    Date birthDate;
    
}

错误:

2021-05-18 23:26:24.139  INFO 12613 --- [  restartedMain] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2021-05-18 23:26:24.164  INFO 12613 --- [  restartedMain] com.assurance.BackApplication            : Started BackApplication in 3.885 seconds (JVM running for 4.409)
2021-05-18 23:26:24.182 ERROR 12613 --- [ntLoopGroup-3-7] o.s.d.m.core.ReactiveMongoTemplate       : Unexpected exception during asynchronous execution

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 2 (BadValue): 'failed to add collation information to index spec for index creation: { key: { username: 1 }, name: "username", unique: true, collation: { locale: "users" }, v: 2 } :: caused by :: Field 'locale' is invalid in: { locale: "users" }' on server ******:27017. The full response is {"operationTime": {"$timestamp": {"t": 1621373184, "i": 8}}, "ok": 0.0, "errmsg": "failed to add collation information to index spec for index creation: { key: { username: 1 }, name: \"username\", unique: true, collation: { locale: \"users\" }, v: 2 } :: caused by :: Field 'locale' is invalid in: { locale: \"users\" }", "code": 2, "codeName": "BadValue", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1621373184, "i": 9}}, "signature": {"hash": {"$binary": {"base64": "*****", "subType": "00"}}, "keyId": *****}}}; nested exception is com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'failed to add collation information to index spec for index creation: { key: { username: 1 }, name: "username", unique: true, collation: { locale: "users" }, v: 2 } :: caused by :: Field 'locale' is invalid in: { locale: "users" }' on server ******:27017. The full response is {"operationTime": {"$timestamp": {"t": 1621373184, "i": 8}}, "ok": 0.0, "errmsg": "failed to add collation information to index spec for index creation: { key: { username: 1 }, name: \"username\", unique: true, collation: { locale: \"users\" }, v: 2 } :: caused by :: Field 'locale' is invalid in: { locale: \"users\" }", "code": 2, "codeName": "BadValue", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1621373184, "i": 9}}, "signature": {"hash": {"$binary": {"base64": "*****", "subType": "00"}}, "keyId": *****}}}
        at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133) ~[spring-data-mongodb-3.1.3.jar:3.1.3]
        at org.springframework.data.mongodb.core.ReactiveMongoTemplate.potentiallyConvertRuntimeException(ReactiveMongoTemplate.java:2819) ~[spring-data-mongodb-3.1.3.jar:3.1.3]
        at org.springframework.data.mongodb.core.ReactiveMongoTemplate.lambda$translateException$91(ReactiveMongoTemplate.java:2802) ~[spring-data-mongodb-3.1.3.jar:3.1.3]
        at reactor.core.publisher.Flux.lambda$onErrorMap$27(Flux.java:6597) ~[reactor-core-3.4.2.jar:3.4.2]
        at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94) ~[reactor-core-3.4.2.jar:3.4.2]
        at reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onError(MonoFlatMapMany.java:255) ~[reactor-core-3.4.2.jar:3.4.2]
        at com.mongodb.reactivestreams.client.internal.AbstractSubscription.onError(AbstractSubscription.java:142) ~[mongodb-driver-reactivestreams-4.1.1.jar:na]
        at com.mongodb.reactivestreams.client.internal.SingleResultCallbackSubscription.lambda$requestInitialData$0(SingleResultCallbackSubscription.java:41) ~[mongodb-driver-reactivestreams-4.1.1.jar:na]
        at com.mongodb.internal.async.client.AsyncMongoCollectionImpl.lambda$createIndex$6(AsyncMongoCollectionImpl.java:872) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.client.AsyncMongoCollectionImpl.lambda$executeCreateIndexes$8(AsyncMongoCollectionImpl.java:925) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:48) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.client.OperationExecutorImpl$2$1$1.onResult(OperationExecutorImpl.java:135) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:48) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.operation.OperationHelper$ReferenceCountedReleasingWrappedCallback.onResult(OperationHelper.java:531) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.operation.CreateIndexesOperation$2$1$1.onResult(CreateIndexesOperation.java:229) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.operation.CreateIndexesOperation$2$1$1.onResult(CreateIndexesOperation.java:226) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.operation.CommandOperationHelper$11.onResult(CommandOperationHelper.java:654) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:48) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor$2.onResult(DefaultServer.java:285) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:48) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.CommandProtocolImpl$1.onResult(CommandProtocolImpl.java:82) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:530) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.UsageTrackingInternalConnection$2.onResult(UsageTrackingInternalConnection.java:142) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:48) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:465) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:440) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:745) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:712) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:582) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:579) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:255) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:214) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:579) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.access$1100(InternalStreamConnection.java:78) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:702) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:687) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:582) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:579) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:255) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:214) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream.handleReadResponse(NettyStream.java:285) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream.access$800(NettyStream.java:69) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:344) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:341) ~[mongodb-driver-core-4.1.1.jar:na]
        at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1533) ~[netty-handler-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1282) ~[netty-handler-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1329) ~[netty-handler-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508) ~[netty-codec-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:447) ~[netty-codec-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) ~[netty-codec-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
        at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'failed to add collation information to index spec for index creation: { key: { username: 1 }, name: "username", unique: true, collation: { locale: "users" }, v: 2 } :: caused by :: Field 'locale' is invalid in: { locale: "users" }' on server ******:27017. The full response is {"operationTime": {"$timestamp": {"t": 1621373184, "i": 8}}, "ok": 0.0, "errmsg": "failed to add collation information to index spec for index creation: { key: { username: 1 }, name: \"username\", unique: true, collation: { locale: \"users\" }, v: 2 } :: caused by :: Field 'locale' is invalid in: { locale: \"users\" }", "code": 2, "codeName": "BadValue", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1621373184, "i": 9}}, "signature": {"hash": {"$binary": {"base64": "*****", "subType": "00"}}, "keyId": *****}}}
        at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175) ~[mongodb-driver-core-4.1.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:454) ~[mongodb-driver-core-4.1.1.jar:na]
        ... 45 common frames omitted

谢谢你的帮助。

共有1个答案

邓英卓
2023-03-14

从错误消息"排序规则:{locale:"用户"}"我假设您在某个地方设置了排序规则。尝试在您的代码中搜索。

 类似资料:
  • 我想通过添加order by params来扩展这个规范。 我找不到解决办法如何实施这一点。你能给我一些建议吗?

  • 1.【强制】 业务上具有唯一特性的字段,即使是多个字段的组合,也必须建成唯一索引。 说明:不要以为唯一索引影响了 insert 速度,这个速度损耗可以忽略,但提高查找速度是明显的;另外,即使在应用层做了非常完善的校验控制,只要没有唯一索引,根据墨菲定律,必然有脏数据产生。 2.【强制】 超过三个表禁止 join。需要 join 的字段,数据类型必须绝对一致;多表关联查询时,保证被关联的字段需要有索

  • 1.【强制】 业务上具有唯一特性的字段,即使是多个字段的组合,也必须建成唯一索引。 说明:不要以为唯一索引影响了 insert 速度,这个速度损耗可以忽略,但提高查找速度是明显的;另外,即使在应用层做了非常完善的校验控制,只要没有唯一索引,根据墨菲定律,必然有脏数据产生。 2.【强制】 超过三个表禁止 join。需要 join 的字段,数据类型必须绝对一致;多表关联查询时,保证被关联的字段需要有索

  • 1.【强制】业务上具有唯一特性的字段,即使是组合字段,也必须建成唯一索引。 说明:不要以为唯一索引影响了insert速度,这个速度损耗可以忽略,但提高查找速度是明显的;另外,即使在应用层做了非常完善的校验和控制,只要没有唯一索引,根据墨菲定律,必然有脏数据产生。 2.【强制】超过三个表禁止join。需要join的字段,数据类型保持绝对一致;多表关联查询时,保证被关联的字段需要有索引。 说明:即使双

  • 本文向大家介绍MySQL 索引的优缺点以及创建索引的准则,包括了MySQL 索引的优缺点以及创建索引的准则的使用技巧和注意事项,需要的朋友参考一下 一、为什么要创建索引呢(优点) 这是因为,创建索引可以大大提高系统的性能。 第一, 通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。 第二, 可以大大加快数据的检索速度,这也是创建索引的最主要的原因。 第三, 可以加速表和表之间的连接,特别是

  • CreateIndexes 根据struct中的tag来创建索引 CreateUniques 根据struct中的tag来创建唯一索引