Skip to content

Instantly share code, notes, and snippets.

@deepal
Last active October 14, 2019 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deepal/22c906924fe85b619f45234f1e90fa05 to your computer and use it in GitHub Desktop.
Save deepal/22c906924fe85b619f45234f1e90fa05 to your computer and use it in GitHub Desktop.
db.createView('tickets', 'ticketevents', [
{
$sort: { timestamp: 1 }
},
{
$group: {
_id: '$tid',
ticketDetails: {
$mergeObjects: '$data'
},
comments: {
$push: {
$cond: [
{$gt: ['$data.comment', null]},
{
author: '$author',
timestamp: 'timestamp',
comment: '$data.comment'
}, null
]
}
},
history: {
$push: {
type: '$type',
author: '$author',
timestamp: 'timestamp',
data: '$data'
}
},
createdBy: {
$first: '$author'
},
createdAt: {
$first: '$timestamp'
}, updatedBy: {
$last: '$author'
}, updatedAt: {
$last: '$timestamp'
}
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
{
tid: '$_id',
status: 'open',
assignee: null,
},
'$ticketDetails',
{
comments: '$comments',
history: '$history',
createdBy: '$createdBy',
createdAt: '$createdAt',
updatedBy: '$updatedBy',
updatedAt: '$updatedAt'
}
]
}
}
},
{
$project: {
tid: 1,
status: 1,
assignee: 1,
title: 1,
description: 1,
comments: {
$setDifference: [
'$comments',
[null]
]
},
history: 1,
createdBy: 1,
createdAt: 1,
updatedBy: 1,
updatedAt: 1
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment