Ich habe Beiträge Sammlung mit verschachteltem Dokument Kommentare. Einfach Dokument Schema sieht so aus:spring-data-mongodb verschachteltes Dokument Projektion
{
"_id" : ObjectId("5867e5e64f768509c59f9392"),
"userId" : 1,
"comments": [
{
"id" : 1,
"text": "a"
},
{
"id" : 2,
"text": "b"
}
]
}
Ich möchte nur den angeforderten Kommentar projizieren. Ich habe eine akzeptable Lösung gefunden. (Nur Kommentar Objekt Rückkehr Lösungen sind auch willkommen)
> db.posts.find({userId: 1, 'comments.id': 1}, {'comments.$': 1});
{ "_id" : ObjectId("5867e5e64f768509c59f9392"), "comments" : [ { "id" : 1, "text" : "a" } ] }
Dann versuche ich es anwenden Repository-Schnittstelle mit Abfrage Anmerkung MongoDB.
@Query(
value = ""
+ "{"
+ " 'userId' : '?1',"
+ " 'comments.id': '?2',"
+ "}",
fields = "'comments.$'"
)
Note findComment(String userId, String commentId);
Aber ich habe Ausnahmeausnahme. Gibt es einen besseren Weg, dies mit Feder-Data-Mongodb zu tun?
Gibt es eine Möglichkeit, nur Kommentardaten zurückzugeben? '{" id ": 1," text ":" a "}' –