Created
May 28, 2019 10:02
-
-
Save mbranicky/c1dc03979dbc173591b4cad4568931aa to your computer and use it in GitHub Desktop.
Movie by ID where joined comments are ordered by date in descending order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets a movie object from the database. | |
* | |
* @param movieId - Movie identifier string. | |
* @return Document object or null. | |
*/ | |
public Document getMovie(String movieId) { | |
if (!validIdValue(movieId)) { | |
return null; | |
} | |
// TODO> Ticket: Get Comments - implement the lookup stage that allows the comments to | |
// retrieved with Movies where comments are ordered by date descending. | |
List<Document> pipeline = asList(new Document("$match", new Document("_id", new ObjectId(movieId))), | |
new Document("$lookup", new Document("from", "comments") | |
.append("let", new Document("movie_id", "$_id")) | |
.append("pipeline", asList(new Document("$match", | |
new Document("$expr", new Document("$and", singletonList(new Document("$eq", asList("$movie_id", "$$movie_id")))))), | |
new Document("$sort", new Document("date", -1)))).append("as", "comments"))); | |
return moviesCollection.aggregate(pipeline).first(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment