解决mongodb查询所有数据出错:Query failed with error code 2 and error message 'Field 'locale' is invalid in:

仅仅只是使用继承自MongoRepository的findAll,却报了如下错误
部分错误信息:
Caused by: com.mongodb.MongoQueryException: Query failed with error code 2 and error message ‘Field ‘locale’ is invalid in: { locale: “comment” }’ on server localhost:27017
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:735)
at com.mongodb.operation.FindOperation 1. c a l l ( F i n d O p e r a t i o n . j a v a : 725 ) a t c o m . m o n g o d b . o p e r a t i o n . O p e r a t i o n H e l p e r . w i t h R e a d C o n n e c t i o n S o u r c e ( O p e r a t i o n H e l p e r . j a v a : 463 ) a t c o m . m o n g o d b . o p e r a t i o n . F i n d O p e r a t i o n . e x e c u t e ( F i n d O p e r a t i o n . j a v a : 725 ) a t c o m . m o n g o d b . o p e r a t i o n . F i n d O p e r a t i o n . e x e c u t e ( F i n d O p e r a t i o n . j a v a : 89 ) a t c o m . m o n g o d b . c l i e n t . i n t e r n a l . M o n g o C l i e n t D e l e g a t e 1.call(FindOperation.java:725) at com.mongodb.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:463) at com.mongodb.operation.FindOperation.execute(FindOperation.java:725) at com.mongodb.operation.FindOperation.execute(FindOperation.java:89) at com.mongodb.client.internal.MongoClientDelegate 1.call(FindOperation.java:725)atcom.mongodb.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:463)atcom.mongodb.operation.FindOperation.execute(FindOperation.java:725)atcom.mongodb.operation.FindOperation.execute(FindOperation.java:89)atcom.mongodb.client.internal.MongoClientDelegateDelegateOperationExecutor.execute(MongoClientDelegate.java:189)
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:143)
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2793)
… 86 more

这个错误困扰了我好几天,百度谷歌也找不到和我类似的错误

最后碰巧地解决了,也是我运气好。

在实体类的注解那里,我一开始写的是:

@Document(collation = "comment")

后来改成:

@Document( "comment")

就可以了。

归根到底应该是我基础不扎实吧。

你可能感兴趣的:(Mongodb)