【发布时间】:2020-05-18 14:51:36
【问题描述】:
我正在编写 RESTful API 以使用 TextQuery 按条件搜索名为“global”的 MongoDB 集合。我的问题是在进行查询时我无法访问嵌套对象字段。
例如这有效:
GET localhost:8080/search?criteria=name:'name'
这不是:
GET localhost:8080/search?criteria=other.othername:'Other Name'
我有 MongoDB json 结构(从 JSON 导入到“全局”集合中作为整个嵌套对象)
[{
"name": "Name",
"desc": "Desc",
"other" {
"othername": "Other Name",
}
},
{
"name": "Name",
"desc": "Desc",
"other" {
"othername": "Other Name",
}
}
]
和类(带有 getter 和 setter 等):
@Document(collection="global")
public class Global{
@TextIndexed
String name;
@TextIndexed
String desc;
Other other;
...
}
public class Other{
String othername;
...
}
我的控制器有方法
@GetMapping("/search")
public Iterable<Global> getByCriteria(@RequestParam("criteria") String criteria) {
...
}
我正在尝试使用
编写文本搜索public Iterable<Global> findByCriteria(String criteria) {
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching(criteria);
TextQuery query = TextQuery.queryText(criteria);
return mongoTemplate.find(query, Global.class);
}
【问题讨论】:
标签: spring mongodb rest spring-boot