0

I tried to retrieve only 3 fields from documents that contain 5 fields using the following @Query.

@Query(value="{'organization':?0,'roleType':?1 }",fields="{'organization' : 0, 'roleType' :0}")
public List<Role> findByOrganizationAndRoleType(String organization, String roleType);

This gives me results like below.

[
  {
    organization: null,
    firstName: "John",
    lastName: "Doe",
    nicNo: "5000",
    roleType: null
  }
]

Is it possible to omit the fields, instead of displaying them as above with "null" values.

  • `null` comes from your Entity class, not MongoDB. Perhaps you need to deal with serializer omitting `null` values [How to tell Jackson to ignore a field during serialization if its value is null?](https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null) – Valijon Jul 06 '20 at 06:46
  • @Valijon thank you, I got it fixed through that. :) – Dinuk Deshan Jul 06 '20 at 08:20

2 Answers2

0

You could use projection to limit the data that is in the result . For instance , if you just need to display the firstname and the lastname of the user then provide an interface like below and use that in your repository interface :

interface NamesOnly {

  String getFirstname();
  String getLastname();
} 


@Repository
interface RoleRepository extends Repository<Role, UUID> {

  @Query(value="{'organization':?0,'roleType':?1 }",fields="{'organization' : 0, 'roleType' :0}")
  public Collection<NamesOnly> findByOrganizationAndRoleType(String organization, String roleType);

}

I haven't tried executing the above code but it provides a general idea about what I am trying to convey. Official doc.

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
0

There are many ways to avoid the null values for fields that are not projected. In addition to the Interface-based Projections you can also try the following two ways. Consider a Pojo class mapped to a collection's document:

public class Pojo {
    private String fld1;
    private Integer fld2;
    private String fld3;
    // constructor, get/set methods, toString(), ...
}

MongoRepository methods which omit fld3:

@Query(value="{'fld1' : ?0 }", fields="{ 'fld3' : 0 }")
List<Pojo> findByFld1(String fld1);

@Query(value="{'fld1' : ?0 }", fields="{ 'fld3' : 0 }")
List<Document> findByFld1Doc(String fld1);

@Query(value="{'fld1' : ?0 }", fields="{ 'fld3' : 0 }")
List<PojoProj> findByFld1Proj(String fld1);

Note the PojoProj is another class with only the fields to be projected.

public class PojoProj {
    private String fld1;
    private Integer fld2;
    // Add get methods for the two fields to be included and the toString()
}

Also, using org.bson.Document class as result type allows omitting the null valued fields in the output.

Run the following queries:

List<PojoProj> result1 = repository.findByFld1Proj("str-1");
List<Document> result2 = repository.findByFld1Doc("str-1");

These two queries will return objects with only the projected fields (omitting the fields with the null values).

prasad_
  • 12,755
  • 2
  • 24
  • 36