简体   繁体   中英

Spring data rest method not able to search on DbRef Id field

I am using mongodb and spring boot in my appliation. In Area collection city is DBRef but spring data rest method is unable to search using city's Id. It's returning empty JSON although for that city I have area's in my database.My Area collection and repository are as below.

@Document(collection = "area")
    public class Area  {

      private String name;

      private String areaCode;

      private String postalCode;

      private String latitude;

      private String longitude;

      private String category;

      @DBRef(lazy = false)
      private City city;

public interface AreaRepo extends MongoRepository<Area, String> {

  @RestResource(path = "byCityId")
  List<Area> findByCityId(@Param(value = "cityId") String cityId);

Same kind of relationship in other collection is working file.

You could add @Query annotation.

@Query("{ 'city': {'$ref': 'City', '$id': { '$oid': ?0 } } }")
List<Area> findByCityId(@Param(value = "cityId") String cityId);

Or find directly with city object

City city = new City(cityId);
List<Area> findByCity(City city);

Could you try searching by City

@Autowired 
private CityRepository cityRepository;

List<Area> findByCity(cityRepository.findByCityId(cityId));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM