我的mongo数据库中有类似的记录。我需要在01/01/2017到30/04/2018之间找到所有用户,并按月对其进行排序。
{
"id": "XV6789",
"valid": true,
"MobileNo": "8612636",
"active": true,
"created": ISODate('2018-04-18T08:28:01.778Z')
}
如果可能的话,响应应该是每月计数的数组!!
答案 0 :(得分:0)
也许这有助于你:
db.YOURCOLLECTION.find({
created: {
'$gte': new Timestamp(new Date(2017, 01, 01), 0),
'$lte': new Timestamp(new Date(2017, 04, 30), 0)
}})
将YOURCOLLECTION替换为您的收藏品,它应该有效。
答案 1 :(得分:0)
您需要使用聚合框架进行此类查询:
db.collection.aggregate([{
$match: { // filter to limit to whatever is of importance
"created": {
$gte: ISODate('2017-01-01T00:00:00.000Z'),
$lte: ISODate('2018-04-30T00:00:00.000Z')
}
}
}, {
$group: { // group by
_id: {
"month": { $month: "$created" }, // month
"year": { $year: "$created" } }, // and year
"count": { $sum: 1 } // and sum up all documents per group
}
}])