Reputation: 13616
I have this repository function:
public interface CommunityRepository extends MongoRepository<Community,String> {
@Query(value="{ 'builder_id' : 1}", fields="{ 'name' : 1, 'description' : 1, 'slogan' : 1, 'address' : 1, 'numberOfHouses' : 1}")
List<Community> findAllByBuilderId(List<String> builderId);
}
Here how I call it from service:
List<String> builderId = new ArrayList<String>();
builderId.add("6127db1d94ae73194a990444");
builderId.add("5f9852ce93834b1fc01f6573");
builderId.add("602db8e7b05b804313f9ft67");
List<Community> communities = underTest.findAllByBuilderId(builderId);
And here Community class:
@Data
@Document("communities")
@Slf4j
public class Community {
@Id
private String id = null;
@NotBlank(message = "Must not be blank")
@Size(max = 50, message = "Max length is 50 characters")
private String name;
@Size(max = 400, message = "Max length is 200 characters")
private String description;
private List<String> tags;
@Field("builder_id")
@Pattern(regexp = "[a-fA-F0-9]{24}", message = "Wrong id format")
private String builderId;
}
The communities list is always empty, but there are a few items associated with that builder id
.
Any idea why the communities list is always empty?
Upvotes: 1
Views: 387
Reputation: 18979
You have not passed a parameter in your query and you have only the static value 1.
{ 'builder_id' : 1}
means search statically for builder_id = 1
What you want is
{ 'builder_id' : { $in: ?0 } }
where you say builder_id must be one of the elements contained in the first parameter provided by your method (index starts from 0) so it is ?0
So you query in the end must be
@Query(value="{ 'builder_id' : { $in: ?0 } } )
List<Community> findAllByBuilderId(List<String> builderId);
If this is not accepted try with {$in: [?0]}
Upvotes: 1