0

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?

1 Answer 1

1

Couple of points

Problem is that when executing that query I get a null. --> Ofcourse you will get null because your repository is TextsRepository, which will look for collection called texts in database which it cannot find.
You have only projects collection in database

@Document(collection = "projects")  

Your Query is correct, but you should be executing this against ProjectRepository.

public interface ProjectRepository extends MongoRepository<Project, String> {

    @Query(value = "{'texts.key': ?0 }", fields = "{ 'texts.key' : 1, 'texts.defaultText': 1 }")
    Project findByKey(String key);   
}

The return type of the query method should be assignable to the managed domain type(in your case Project).

Sign up to request clarification or add additional context in comments.

1 Comment

I thought I didn't end up submitting this question! I quickly learnt it short after posting. Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.