1

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}?

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So, basically 1 = TRUE, 0 = FALSE?
1 - include field in the result, 0 - exclude. Or, if you do not provide 'fields' falue, all document fields will be included.
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.