Skip to content

Instantly share code, notes, and snippets.

@rhenning
Last active August 29, 2015 14:07
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 rhenning/e798e8693c5257fd4de0 to your computer and use it in GitHub Desktop.
Save rhenning/e798e8693c5257fd4de0 to your computer and use it in GitHub Desktop.
Mongo aggregations and date to ObjectId
// run like: nohup mongo --quiet neat_prod > entity_users.js
days_ago = 120
date_after = new Date(new Date().setDate(new Date().getDate() - days_ago))
secs_after = Math.floor(date_after.getTime() / 1000)
oid_after = ObjectId(secs_after.toString(16) + "0000000000000000")
rs.slaveOk()
c = db.entities.find(
{ _id: { $gte: oid_after } },
{ _id: 0 , account_id: 1 }
)
while (c.hasNext()) {
printjson(c.next())
}
/*
printjson(
db.entities.distinct(
'account_id',
{ _id: { $gte: oid_after } }
)
)
*/
/*
printjson(
db.entities.aggregate(
{ $match: { _id: { $gte: oid_after } } },
{ $group: { _id: '$account_id', count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $project: { _id: 0, account_id: '$_id', entities_created_count: '$count' } }
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment