I have the following two documents:
@Document(collection = "projects")
public class Project {
@Id
private String id;
private String name;
@Indexed(unique = true)
private String slug;
private List<Component> components;
private List<Feature> features;
private List<Text> texts;
// constructors, getters and setters omitted
}
And:
public class Text {
private String key;
private String defaultText;
private String comment;
private String featureUsing;
private List<String> componentsUsing;
// constructors, getters and setters omitted
}
Basically I have an array of texts inside a project.
Currently this is the only document I have:
{
"_class": "org.aribeiro.i18n.entities.Project",
"_id": "5a64b8b65aa0334ada6eced6",
"components": [
{
"name": "Find and Ring portal",
"slug": "find-and-ring-portal"
}
],
"features": [
{
"name": "Find and Ring",
"slug": "find-and-ring"
}
],
"name": "Project 2",
"slug": "project-2",
"texts": [
{
"comment": "This is to show the title",
"componentsUsing": [
"find-and-ring-portal"
],
"defaultText": "Find and Ring",
"featureUsing": "find-and-ring",
"key": "findringportal.title"
}
]
}
I want to get only the texts part that match the following filter:
{'texts.key': 'findringportal.title' }
So I configured this query in my TextsRepository:
@Query(value = "{'texts.key': ?0 }", fields = "{ 'texts.key' : 1, 'texts.defaultText': 1 }")
Text findByKey(String key);
TextsRepository is:
public interface TextsRepository extends MongoRepository<Text, String> {
@Query(value = "{'texts.key': ?0 }", fields = "{ 'texts.key' : 1, 'texts.defaultText': 1 }")
Text findByKey(String key);
}
Problem is that when executing that query I get a null. The only way to get some data is by making it return a Project instance and only the fields defined are returned. But isn't there a way to make it marshal to the nested entities?