A few days ago in the use of mongo query encountered a very magical error message, and the error message only in the server after the first query will be generated, the subsequent use of normal again. Investigations into the problem have been fruitless for a long time. Recently, the breakpoint investigation again, and finally found the cause of the specific error.

The error is caused by the use of a Chinese colon (:) in the annotation definition index. The underlying code cannot parse the colon, so the error is reported.

When our project’s server starts its first query, it iterates through the index annotated in the code. If the index does not exist in Mongo, it creates an index for Mongo and does not process it if it already exists. The result is that every time the server is started, the first query will always report the error in the question.

If you have friends with similar problems, you can check in this direction.

The approximate stack is as follows:

org.bson.json.JsonParseException: Invalid JSON input. Position: 16. Character: ':'. at org.bson.json.JsonScanner.nextToken(JsonScanner.java:99) at org.bson.json.JsonReader.popToken(JsonReader.java:492) at org.bson.json.JsonReader.readBsonType(JsonReader.java:130) at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149) at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45) at org.bson.Document.parse(Document.java:105) at org.bson.Document.parse(Document.java:90) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveCompoundIndexKeyFromStringDefiniti on(MongoPersistentEntityIndexResolver.java:376) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.createCompoundIndexDefinition(MongoPersis tentEntityIndexResolver.java:345) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.lambda$createCompoundIndexDefinitions$2(MongoPersistentEntityIndexResolver.java:327)
at java.util.stream.ReferencePipeline$3The $1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.createCompoundIndexDefinitions(MongoPersi stentEntityIndexResolver.java:328) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.potentiallyCreateCompoundIndexDefinitions (MongoPersistentEntityIndexResolver.java:211) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexForEntity(MongoPersistentEnti tyIndexResolver.java:109) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexFor(MongoPersistentEntityInde xResolver.java:90) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEn tityIndexCreator.java:134) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndex Creator.java:127) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIn dexCreator.java:111) at com.exe.mongo.client.factory.MongoMappingEventPublisher.publish(MongoMappingEventPublisher.java:30) at com.exe.mongo.client.factory.MongoMappingEventPublisher.publishEvent(MongoMappingEventPublisher.java:18) at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:420) at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:229) at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:172) at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:85) at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:70)Copy the code