Skip to content

Instantly share code, notes, and snippets.

@icu0755
Created August 21, 2023 09:43
Show Gist options
  • Save icu0755/e5886653c3d1a7e699b69ea298cdb218 to your computer and use it in GitHub Desktop.
Save icu0755/e5886653c3d1a7e699b69ea298cdb218 to your computer and use it in GitHub Desktop.
Sort and limit mongo collection by parent docs then add corresponding children docs
[
{
'$match': {
'app': 'nametestsig',
'lang': 'en',
'label_name': '',
'label_value': '',
'content_id': {
'$ne': '0'
}
}
}, {
'$sort': {
'kpis.sb_clicks_t': -1
}
}, {
'$limit': 2
}, {
'$lookup': {
'from': 'results',
'let': {
'parentApp': '$app',
'parentLang': '$lang',
'parentContentId': '$content_id'
},
'pipeline': [
{
'$match': {
'$expr': {
'$and': [
{
'$eq': [
'$app', '$$parentApp'
]
}, {
'$eq': [
'$lang', '$$parentLang'
]
}, {
'$eq': [
'$content_id', '$$parentContentId'
]
}, {
'$ne': [
'$label_name', ''
]
}, {
'$ne': [
'$label_value', ''
]
}
]
}
}
}
],
'as': 'result'
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment