Skip to content

Instantly share code, notes, and snippets.

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 technoknol/1fd09a97b3c0f65fb6921b5e4107ff45 to your computer and use it in GitHub Desktop.
Save technoknol/1fd09a97b3c0f65fb6921b5e4107ff45 to your computer and use it in GitHub Desktop.
Mogodb Cookbook
// to prevent empty objects being pushed in resule like 'eventWinners' : [ {} ]
// if there are no winners found, after lookup and unwind.
$group: {
_id: '$_id',
root: {
$mergeObjects: '$$ROOT',
},
eventWinners: {
$push: {
$cond: {
if: { $eq: ["$eventWinners", {}] },
then: '$$REMOVE',
else: '$eventWinners',
}
}
},
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment