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

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Maksim
  • 16,635
  • 27
  • 94
  • 135

3 Answers3

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.

Ivan Sharamet
  • 317
  • 4
  • 15
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);
akinKaplanoglu
  • 728
  • 2
  • 8
  • 26
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.

Samba
  • 181
  • 2
  • 4