Created
February 24, 2020 11:25
-
-
Save nyancodeid/44f2054dc3da2f7fdfdfb72007dafa3c to your computer and use it in GitHub Desktop.
Playlists Aggregation
This file contains hidden or 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
| function getPlaylist(userId, playlistId) { | |
| return database.playlists.aggregate([{ | |
| $match: { | |
| _id: ObjectId(playlistId), | |
| user_id: userId | |
| } | |
| }, { | |
| $unwind: { | |
| path: "$audios", | |
| preserveNullAndEmptyArrays: true | |
| } | |
| }, { | |
| $lookup: { | |
| from: "audios", | |
| localField: "audios", | |
| foreignField: "_id", | |
| as: "audios" | |
| } | |
| }, { | |
| $unwind: { | |
| path: "$audios", | |
| preserveNullAndEmptyArrays: true | |
| } | |
| }, { | |
| $sort: { | |
| "audios.created_at": -1 | |
| } | |
| }, { | |
| $group: { | |
| _id: "$_id", | |
| title: { | |
| $first: '$title' | |
| }, | |
| description: { | |
| $first: '$description' | |
| }, | |
| isPrivate: { | |
| $first: '$isPrivate' | |
| }, | |
| updated_at: { | |
| $first: '$updated_at' | |
| }, | |
| audios: { | |
| $push: "$audios" | |
| }, | |
| type: { | |
| $first: '$type' | |
| } | |
| } | |
| }]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment