Skip to content

Instantly share code, notes, and snippets.

@darkmavis1980
Created July 19, 2019 10:47
Show Gist options
  • Save darkmavis1980/6391593263efeb11752c304a46d3080f to your computer and use it in GitHub Desktop.
Save darkmavis1980/6391593263efeb11752c304a46d3080f to your computer and use it in GitHub Desktop.
mongo_aggregate_and_filter
/**
* This will filter down to the matches with a specific player, with the matches being played after a specific date,
* and it will show only the aggregate matches after that date ignoring the previous ones
*/
var dateFilter = new ISODate("2019-06-01T20:15:31Z");
db.getCollection('competitions').aggregate([
{
$match: {
players: ObjectId("5c71c4c1185c610046f17911"),
'matches.date': {
$gte: dateFilter
}
}
},
{
$project: {
matches: {
$filter: {
input: '$matches',
as: 'item',
cond: {
$gte: ['$$item.date', dateFilter]
}
}
}
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment