如何使用MongoDB核心查询API选择符合我的条件的字段

huangapple go评论45阅读模式
英文:

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);
 }

huangapple
  • 本文由 发表于 2020年8月9日 22:25:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63327476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定