Created
December 22, 2016 10:39
-
-
Save matthlavacka/c303755c45585f01c8db0ab2a314f345 to your computer and use it in GitHub Desktop.
Count first time Requests per month per user type
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
const date1 = new Date("November 1, 2016 00:00:01"); | |
const date2 = new Date("November 30, 2016 23:59:59"); | |
db.requests.aggregate([ | |
{ | |
$match: { | |
status: 'finished', | |
booker: {$exists: true}, | |
} | |
}, | |
{ | |
$group: { | |
_id: '$booker', | |
requests: { | |
$push: { | |
_id: '$_id', | |
date: '$updatedAt' | |
} | |
} | |
} | |
}, | |
{ | |
$match: { | |
'requests.date': { | |
$not: { | |
$gte: date2, | |
} | |
} | |
} | |
}, | |
{ | |
$unwind: '$requests' | |
}, | |
{ | |
$match: { | |
'requests.date': { | |
$gte: date1 | |
} | |
} | |
}, | |
{ | |
$group: { | |
_id: '$_id' | |
} | |
} | |
]).map(function (x) { | |
return [ | |
x, | |
db.users.find({_id: x._id}).toArray(), | |
] | |
}).forEach(function (x) { | |
print(JSON.stringify(x[1][0].profile.type)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment