问题
I'm playing with Spring Data Mongo Query and wondering about the field property parameters. Here is the example that I got from the documentation:
public interface PersonRepository extends MongoRepository<Person, String>
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
List<Person> findByThePersonsFirstname(String firstname);
}
The question is: What is the meaning of 1 in { 'firstname' : 1, 'lastname' : 1}?
回答1:
1 means that both 'firstname' and 'lastname' will be included in the resulted document. For example, if you have 'salary' field you can exclude it from result by typing 'salary': 0.
回答2:
You can use MongoTemplate for querying.First you declare query and after that you can declare criteria.Below is an example :
Criteria criteria = Criteria.where("kademeler.isemriId").is(isemriNo)
.and("ogag").is(1);
Query query = new Query(criteria);
query.fields().exclude("salary"); //for excluding a field, this is "salary" for you
List<AboneAriza> result = mongoTemplate.find(query, AboneAriza.class);
回答3:
Just to add, the id of the document is also returned by default, so this in detail will mean, firstname, lastname along with _id of the document will be return, and as some one already answered setting a field to zero will not return that particular field when the document is being returned.
来源:https://stackoverflow.com/questions/16739210/spring-data-mongo-query-field-parameters