Java 如何选择Mongodb核心查询API与我的条件匹配的字段
我只需要从符合条件的集合中选择一个字段名(categoryName)。而是从集合中返回以下代码的所有字段:Java 如何选择Mongodb核心查询API与我的条件匹配的字段,java,mongodb-query,spring-data-mongodb,Java,Mongodb Query,Spring Data Mongodb,我只需要从符合条件的集合中选择一个字段名(categoryName)。而是从集合中返回以下代码的所有字段: import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; public ServiceLis
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
public ServiceList findOneByCategoryName(String categoryName) {
// TODO Auto-generated method stub
Query query = new Query() ;
query.addCriteria(Criteria.where("categoryName").is(categoryName));
return (ServiceList) mongoTemplate.findOne(query,ServiceList.class);
}
ServiceList.java
@Document(collection = "services")
public class ServiceList {
@Id
private String id;
@Field("categoryName")
@JsonProperty("categoryName")
private String categoryName;
@Field("serviceTypes")
@JsonProperty("Service Types")
private List<ServiceType> serviceTypes;
public String getMfServicesId() {
return id;
}
public void setMfServicesId(String mfServicesId) {
this.id = mfServicesId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public List<ServiceType> getServiceTypes() {
return serviceTypes;
}
public void setServiceTypes(List<ServiceType> serviceTypes) {
this.serviceTypes = serviceTypes;
}
}
@文档(collection=“services”)
公共类服务列表{
@身份证
私有字符串id;
@字段(“类别名称”)
@JsonProperty(“类别名称”)
私有字符串categoryName;
@字段(“服务类型”)
@JsonProperty(“服务类型”)
私有列表服务类型;
公共字符串getMfServicesId(){
返回id;
}
public void setMfServicesId(字符串mfServicesId){
this.id=mfServicesId;
}
公共字符串getCategoryName(){
返回类别名称;
}
公共无效setCategoryName(字符串categoryName){
this.categoryName=categoryName;
}
公共列表getServiceTypes(){
返回服务类型;
}
公共void设置服务类型(列出服务类型){
this.serviceTypes=serviceTypes;
}
}
对于此代码,所有字段都从集合中返回。我不知道如何选择我选择显示的特定字段。我认为这可能会起作用,使用
字段()
方法:
Query query = new Query();
query.addCriteria(Criteria.where("categoryName").is(categoryName));
query.fields().include("categoryName");
query.fields().exclude("id"); // Not sure you have to exclude Id explicitly
return (String) mongoTemplate.findOne(query,String.class);
以下代码用于使用“包含”功能从文档中选择和显示特定列
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
public ServiceList findOneByCategoryName(String categoryName) {
Query query = new Query() ;
query.addCriteria(Criteria.where("categoryName").is(categoryName));
query.fields().include("categoryName");
return (ServiceList) mongoTemplate.findOne(query,ServiceList.class);
}
您是否可以包括您的文档模型,如ServiceList和您正在使用的任何嵌入式文档?这样做会导致错误。不能同时使用Include和exclude方法。其中任何一个都可以使用。但我知道您的ideaId必须被明确排除,因为mongo默认使用以下代码获取Id字段:query.fields().exclude(“Id”);