0

Following is the entity class for "Projects".I need to fetch the field "title" and "projectName" of all the documents with projectName as "xyz".The field name "title" in the retrieved documents should be renamed as "renamedTitle" while retrieving.

@Document (collection = "projectCollection")
@Data
@NoArgsConstructor
@AllArgsConstructor
 public class Project {
@Id 
private String projectId; 
private String projectName;
private String title;

}

 if the document collection is:
    {"_id":"1","projectName":"alpha","title":"start"},
    {"_id":"2","projectName":"beta","title":"mid"},
    {"_id":"3","projectName":"xyz","title":"last"}
The output expected is:
{"projectName":"xyz","renamedTitle":"last"}

1 Answers1

0

Try this,

@Json(name = "title")
private String renamedTitle;

or

@Json(name = "renamedTitle")
private String title;

One of the above should solve. Please let us know which one gave expected output.

reflexdemon
  • 836
  • 8
  • 21
  • Hi @reflexdemon, thanks for the quick response but i am expecting something by using aggregation/projection. – Allen Shaju Nov 08 '19 at 03:07
  • Something like mongodb equivalent of SELECT field AS `anothername` in spring boot – Allen Shaju Nov 08 '19 at 03:16
  • In that case you should use $project and on spring boot use `@Query("{'name : ?0'}")` or use direct mongo connection using Mongo Templates. See https://stackoverflow.com/a/23784932/3560866 and https://www.devglan.com/spring-boot/spring-data-mongodb-queries – reflexdemon Nov 08 '19 at 15:02