Skip to content

Instantly share code, notes, and snippets.

@mbranicky
Created May 28, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbranicky/c1dc03979dbc173591b4cad4568931aa to your computer and use it in GitHub Desktop.
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
/**
* 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