使用Spring Data整合MongoDB,查询某集合中的所有文档时报错,报错详细信息如下:

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 2 and error message ‘Field ‘locale’ is invalid in: { locale: “userList” }’ on server 121.41.57.xxx:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 2 and error message ‘Field ‘locale’ is invalid in: { locale: “userList” }’ on server xxx:27017

报错原因(巨坑):@Document使用collation属性指定了某个集合,实际上应该是collection,他们俩个名字很像,很容易弄错!

在这里插入图片描述

/**
 * @description:
 * @date: 2022/4/3 19:26
 */
@Document(collection = "userList") // collection一定不要写错了
public class UserVO {
    private String _id;
    private String name;
    private String pwd;
    private Integer age;
    private Integer rownum;
    ...
}

改回来后就不报错了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐