JavaScript query for data in MongoDB by date _mongodb

Source: Internet
Author: User
Tags mongodb

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 '} ]  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.