Skip to content

Instantly share code, notes, and snippets.

@nyancodeid
Created February 24, 2020 11:25
Show Gist options
  • Select an option

  • Save nyancodeid/44f2054dc3da2f7fdfdfb72007dafa3c to your computer and use it in GitHub Desktop.

Select an option

Save nyancodeid/44f2054dc3da2f7fdfdfb72007dafa3c to your computer and use it in GitHub Desktop.
Playlists Aggregation
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