gpt4 book ai didi

java - Spring 数据休息方法无法在 DbRef Id 字段上搜索

转载 作者:行者123 更新时间:2023-12-02 12:00:10 30 4
gpt4 key购买 nike

我在我的应用程序中使用 mongodb 和 spring boot。在区域集合中,城市是 DBRef,但 Spring Data Rest 方法无法使用城市的 Id 进行搜索。它返回空 JSON,尽管对于该城市,我的数据库中有区域。我的区域集合和存储库如下。

@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);

其他集合中的同类关系是工作文件。

最佳答案

您可以添加@Query注释。

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

或者直接用city对象查找

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

关于java - Spring 数据休息方法无法在 DbRef Id 字段上搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323046/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com