Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created December 30, 2018 04:59
Show Gist options
  • Select an option

  • Save tabvn/c00397f400207c407b0415308d8cb5cf to your computer and use it in GitHub Desktop.

Select an option

Save tabvn/c00397f400207c407b0415308d8cb5cf to your computer and use it in GitHub Desktop.
db.getCollection('person_report').aggregate([
{
$match: {
_id: {$in: [ObjectId("5995d36e61e71c54a042d859")]},
roles: {
$elemMatch: {
roleId: {$in: [ObjectId("599586c861e71c54a042d84b")]},
"production.eventDate": {$gte: ISODate("2019-01-02T17:00:00.000Z")},
"production.eventDate": {$lte: ISODate("2017-01-02T17:00:00.000Z")},
}
}
}
},
{
$project: {
_id: true,
firstName: true,
lastName: true,
kind: true,
roles: {
$filter: {
input: "$roles",
as: "role",
cond: {
$and: [
{ $gte: [ "$$role.production.eventDate", ISODate("2017-06-21T16:00:00.000Z") ] },
{ $lte: [ "$$role.production.eventDate", ISODate("2020-12-11T17:00:00.000Z") ] },
{ $eq: [ "$$role.roleId", ObjectId("599586c861e71c54a042d84b") ] },
]
}
}
}
}
},
{
$project: {
_id: true,
firstName: true,
lastName: true,
kind: true,
roles: true,
ratings: {
$filter: {
input: "$roles",
as: "r",
cond: {$gte: ["$$r.ratings.score", 0]}
}
},
totalShows: {$size: "$roles"},
}
},
{
$project: {
_id: true,
firstName: true,
lastName: true,
kind: true,
ratings: {$avg: "$ratings.ratings.score"},
totalShows: {$size: "$roles"},
}
},
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment