gpt4 book ai didi

java - 使用 Spring MongoOperations 根据查询获取所有文档

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:59 25 4
gpt4 key购买 nike

我的用户 POJO 看起来像:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "User")
public class User {
@Id
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

我能够根据这个查询获得单个文档:

Query searchQuery = new Query(Criteria.where("name").is("shashi"));
mongoOperations.findOne(searchQuery, User.class);

我想获取此查询的所有文档。一些方法调用,例如 mongoOperations.findAll(searchQuery, User.class);我该怎么做?

最佳答案

你有两个选择,

选项 1:

List<User> listUser = mongoOperations.find(searchQuery, User.class);
System.out.println("Number of user = " + listUser.size());

选项 2:

List<User> listUser = mongoOperation.findAll(User.class);
System.out.println("Number of user = " + listUser.size());

注:更多详情可以引用THIS LINK

关于java - 使用 Spring MongoOperations 根据查询获取所有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38006527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com