英文:
How to select a field with Mongodb Core Query API matching my criteria
问题
我需要从我的集合中选择只一个字段名(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 ServiceList findOneByCategoryName(String categoryName) {
Query query = new Query();
query.addCriteria(Criteria.where("categoryName").is(categoryName));
return (ServiceList) mongoTemplate.findOne(query, ServiceList.class);
}
对于这段代码,会从集合中返回所有字段。我不知道如何选择要显示的特定字段。
英文:
I have a need to select only one field name (categoryName) from my collection matching my criteria. Instead all fields are returned from the collection for the following code:
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;
}
}
For this code, all fields are returned from the collection. I don't know how to select specific field that i chose to display.
答案1
得分: 1
我认为这可能有效,使用fields()
方法:
Query query = new Query();
query.addCriteria(Criteria.where("categoryName").is(categoryName));
query.fields().include("categoryName");
query.fields().exclude("id"); // 不确定是否需要明确排除Id
return (String) mongoTemplate.findOne(query, String.class);
英文:
I think this might work, using the fields()
method:
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);
答案2
得分: 0
以下代码使用 include 函数从文档中选择并显示特定列,并已经运行成功。
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);
}
英文:
Following code worked to select and display a specific column from a document using include function.
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论