Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.