Skip to content

Instantly share code, notes, and snippets.

@mrwithersea
Created July 28, 2017 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrwithersea/3449c3448d9ce698ae3a21491b71d0d8 to your computer and use it in GitHub Desktop.
Save mrwithersea/3449c3448d9ce698ae3a21491b71d0d8 to your computer and use it in GitHub Desktop.
const { ObjectID } = require('mongodb');
const moment = require('moment');
const thisWeek = moment().startOf('isoWeek').unix();
const cursor = db.collection('records').aggregate(
{
$project: {
thisWeek: {
$cond: {
if: {
$gt: ['$_id', ObjectID.createFromTime(thisWeek) ]
},
then: true,
else: false
}
},
}
},
{
$group: {
_id: {
thisWeek: '$thisWeek',
},
count: {
$sum: 1
}
}
},
{
$project: {
thisWeek: '$_id.thisWeek',
count: '$count',
_id: false
}
},
{ cursor: { batchSize: 1 } });
const docs = yield cursor.toArray();
console.log(docs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment