ianaz
ianaz

Reputation: 2580

Spring Data Mongo nested id won't work

Does Spring Data treats mongo nested "id" attributes differently? I explain my problem: I have collection matches with the following structure

"teams":  [
        {
            "id" : "5601",
            "name" : "FC Basel"
        },
        ... // more
    ]

When I want to retrieve all the matches which has team id 5601 I execute the following query

db.matches.find({ "teams.id" : "5601"})

Which works perfectly and returns some objects.

When I make a method

public List<MatchMongo> findByTeams_id(String id);

on my MatchRepository interface I get 0 results back while there are. Logs shows

Created query Query: { "teams.id" : "5601"}, Fields: null, Sort: null

find using query: { "teams.id" : "5601"} fields: null for class: class
MatchMongo in collection: matches

So the query he makes seems to be the right one... :S Trying with other fields (referee.name for ex.) works.

I even tried with the @Query annotation, but can't get it to work

Is there another solution? Is this a bug or am I doing something wrong?

Upvotes: 2

Views: 2039

Answers (1)

ianaz
ianaz

Reputation: 2580

Oh found the solution:

MatchMongo had List<TeamMongo> teams; on whereI had

@Id
private String id;
@Field(value = "id")
private String teamIdAttr;

So the method should be called

public List<MatchMongo> findByTeams_teamIdAttr(String id);

Never thought the method name should reflect objects attributes instead of collection structure

Thanks @martin-baumgartner your comment helped to solve this :)

Upvotes: 2

Related Questions