GROUP BY date Aggregate query date statistics daily data (amount of information)
1
{
"_id": ObjectId ("557ac1e2153c43c320393d9d"),
"Msgtype": "Text",
"Sendtime": Isodate ("2015-06-12t11 : 26:26.000z ")
}
2
{
"_id": ObjectId ("557ac1ee153c43c320393d9e"),
"Msgtype": "Text",
"Sendtime": Isodate ("2015-06-12t11 : 26:38.000z ")
}
3
{
"_id": ObjectId ("557ac2012de5d32d213963b5"),
"Msgtype": "Text",
"Sendtime": Isodate (" 2015-06-12t11:26:56.000z ")
}
4
{
"_id": ObjectId ("557ac978bb31196e21d23868"),
"Msgtype": "Text",
"Sendtime": Isodate (" 2015-06-12t11:58:47.000z ")
}
5
{
"_id": ObjectId ("557ac9afbb31196e21d23869"),
"Msgtype": "Text",
"Sendtime": Isodate ("2015-06-12t11 : 59:43.000z ")
}
SQL here
Db.getcollection (' Wechat_message '). Aggregate (
[
{ $project: {day: {$substr: ["$sendTime", 0, 10]}}},< c10/>{ $group : {_id: ' $day ', number: {$sum: 1}}},
{ $sort : {_id:-1}}
]
result here
"Result": [
{
"_id": "2015-07-06",
"number": 13.0000000000000000
},
{
"_id": "2015-07-05",
"number": 3.0000000000000000
},
{"
_id": "2015-07-03",
"number": 10.0000000000000000
},
{
"_id": "2015-07-02",
" Number ": 29.0000000000000000
},
]
3 Ways to query all the information for one day, query by date
MongoDB's query really makes people difficult to ponder, on the inquiry one day information, all need to spend a few kung fu to do.
The first way:
Coll.aggregate ([
{$project: {senddate: {$substr: [' $sendTime ', 0,]}, Sendtime:1, Content:1}},
{$match: { Senddate: ' 2015-07-05 '}},
]
The second way (variation of the first kind):
Coll.aggregate ([
{$match: {' sendtime ': {' $gte ': new Date (' 2015-07-05 '), ' $lt ': new Date (' 2015-07-06 ')}}}
The Third Way (variation of the second kind):
Coll.aggregate ([
{$match: {' sendtime ': {' $gte ': new Date (' 2015-07-05 00:00:00 '), ' $lte ': new Date (' 2015-07-05 23:59:59 ')}}}
The query results are as follows (show one way: Other shows are slightly different):
[{_id:5599b09bc16aac90e9fb7995, senddate: ' 2015-07-05 '},
{_id:5599b161c16aac90e9fb7996, senddate: ' 2015-07-05 '},
{_id:5599b161c16aac90e9fb7997, senddate: ' 2015-07-05 '} ]