[英]Spring Boot MongoRepository findAll() returns empty array
I have a Spring Boot application connected to the local Mongo database, but when I try to fetch all the documents from a collection by requesting http://localhost:8080/api/v1/users
it returns an empty array.我有一个连接到本地 Mongo 数据库的 Spring 引导应用程序,但是当我尝试通过请求
http://localhost:8080/api/v1/users
从集合中获取所有文档时,它返回一个空数组。 I don't have any connection errors as well.我也没有任何连接错误。 I've read that the problem could've been with collection names, but when I specified collection name (and now collection name in MongoDB is the same as in the model), it still returns an empty array.
我已经读到问题可能出在集合名称上,但是当我指定集合名称时(现在 MongoDB 中的集合名称与模型中的相同),它仍然返回一个空数组。 What else could've been wrong?
还有什么可能是错的?
Model: Model:
@Document(collection = "users")
public class User {
@Id
private String id;
private String username;
private String email;
private String password;
private String birthdate;
private int chest;
private int weight;
private int height;
private boolean gender;
// constructors, getters and setters omitted
}
Controller: Controller:
@RestController
@RequestMapping("api/v1/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<User> findAll() {
return userService.find();
}
}
Service:服务:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> find() {
return userRepository.findAll();
}
}
Repository:存储库:
public interface UserRepository extends MongoRepository<User, String> {
public User findByEmail(String email);
}
Straight from MongoDB:直接来自 MongoDB:
> db.users.find().pretty()
{
"_id" : ObjectId("60002a15803d9027a459b7b6"),
"username" : "name",
"email" : "zaaas@sasasfa.aaa",
"birthdate" : "1999-10-2",
"weight" : 67,
"height" : 175,
"chest" : 87,
"gender" : true,
"__v" : 0
}
{
"_id" : ObjectId("60002be21da6d929fa45c9f2"),
"username" : "aye",
"email" : "zaaas@sasasfsdsa.aaa",
"birthdate" : "1999-10-2",
"weight" : 65,
"height" : 180,
"chest" : 80,
"gender" : true,
"__v" : 0
}
The problem was with my connection properties.问题出在我的连接属性上。
Before:前:
# application.yml, started using yaml just for better readability,
# should work as fine with application.properties
spring:
data:
mongodb:
url: mongodb://localhost:27017/{dbname}
After:后:
# application.yml
spring:
data:
mongodb:
host: localhost
port: 27017
database: {dbname}
The issue arises because the collection name is not explicitly given in your model.Can you change your code like below出现问题是因为您的 model 中没有明确给出集合名称。您可以像下面这样更改代码吗
@Document(collection = "User")
public class User {
@Id
private String id;
private String username;
private String email;
private String password;
private String birthdate;
private int chest;
private int weight;
private int height;
private boolean gender;
// constructors, getters and setters omitted
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.