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) –