-
-
Save dpjayasekara/9c3089f4fc809b4fd8a2dce66c9c3ccd to your computer and use it in GitHub Desktop.
This file contains 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
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