1
我需要找到只需要调用dados
的特定字段,这个工作很完美,但将所有字段返回给我。
如何才能返回dados
字段?为什么我的查询返回所有字段?
@Query("{\"dados\": {$ne: null}}, {\"dados\": 1}")
List<Contato> findOnlyDados();
实体(没有getter和setter)
@Document
public class Contato {
@Id
private String id;
String nome;
List<Info> dados;
@DBRef
Agencia agencia;
static class Info {
String campo;
String valor;
}
}
当我做了GET请求
{
"_links": {
"self": {
"href": "http://localhost:8181/api/contatos/search/findOnlyDados"
}
},
"_embedded": {
"contatos": [
{
"nome": "Contato one",
"dados": [
{
"campo": "teste um",
"valor": "fmdsf"
},
{
"campo": "dois",
"valor": "bdfbfd"
}
],
"_links": {
"self": {
"href": "http://localhost:8181/api/contatos/55b66de7ccf21a7a1cfcd403"
},
"agencia": {
"href": "http://localhost:8181/api/contatos/55b66de7ccf21a7a1cfcd403/agencia"
}
}
},
{
"nome": "Contato bgf",
"dados": [
{
"campo": "teste jghj",
"valor": "ytrf"
},
{
"campo": "jhjhn",
"valor": "bdfbfd"
}
],
"_links": {
"self": {
"href": "http://localhost:8181/api/contatos/55b66dfeccf21a7a1cfcd404"
},
"agencia": {
"href": "http://localhost:8181/api/contatos/55b66dfeccf21a7a1cfcd404/agencia"
}
}
}
]
}
}
我相信问题是你发送的查询和字段都是单个字符串。尝试'@Query(value =“{'dados':{$ ne:null}}”,fields =“{'dados':1}”)'。受[spring-data示例]启发(http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mongo.repositories.html) –