I'm trying to get all the values of a specific field to a array.
This is the document structure:
{
"title": "testone",
"docs": [
{
"fileId": "123",
"fileName": "helloworld.txt"
},
{
"fileId": "124",
"fileName": "thisisalongtext.docx",
}],
}
Currently I've implemented following
Docs Class :
public class Docs implements Serializable {
private String fileId;
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
}
Assignment Class:
@Document(collection = "assignemnt")
public class Assignment{
private String title;
private List<Docs> docs;
public String getTitle() {
return title;
}
public List<Docs> getDocs() {
return docs;
}
public void setDocs(List<Docs> docs) {
this.docs= docs;
}
@Override
public String toString() {
return "Assignment [title=" + title
+ ", docs=" + docs+ "]";
}
}
RepositoryImplementation:
public List<Assignment> getAssignments(final String uID) {
Query query = Query.query(Criteria.where(CREATEDBY).is(uID)
.orOperator(Criteria.where("docs.0").exists(true),
(Criteria.where("versionchecked").is(true))));
return mongoTemplate.find(query, Assignment.class);
}
Current method returns the following response:
{
"title": "assignemnt123",
"docs": [
{
"fileId": "132"
},
{
"fileId": "123"
},
{
"fileId": "213"
}
]
}
Response I want to create:
{
"title": "assignemnt123",
"docs": [ "132" , "123", "213" ]
}
Is there a way to achieve this expected response?