国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

JavaScript按日期查詢MongoDB中的數(shù)據(jù)的要點示例

2020-10-29 18:49:54
字體:
供稿:網(wǎng)友

group by date 聚合查詢?nèi)掌?統(tǒng)計每天數(shù)據(jù)(信息量)

{   "_id" : ObjectId("557ac1e2153c43c320393d9d"),   "msgType" : "text",   "sendTime" : ISODate("2015-06-12T11:26:26.000Z") } 

 

{   "_id" : ObjectId("557ac1ee153c43c320393d9e"),   "msgType" : "text",   "sendTime" : ISODate("2015-06-12T11:26:38.000Z") } 

 

{   "_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") } 

 

{   "_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] }}},         {  $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種方法,根據(jù)日期查詢
mongodb的查詢真讓人難以琢磨,就查詢單天信息,都需要花費一番功夫才行。 
 
第一種方式:  

coll.aggregate([      {$project:{sendDate: {$substr: ['$sendTime', 0, 10]}, sendTime: 1, content:1}},      {$match:{sendDate: '2015-07-05'}},     ]) 

  
 
第二種方式(第一種的變異):  

coll.aggregate([      {$match: {'sendTime': {'$gte': new Date('2015-07-05'), '$lt': new Date('2015-07-06')}}} 

 
第三中方式(第二種的變異): 

coll.aggregate([      {$match: {'sendTime': {'$gte': new Date('2015-07-05 00:00:00'), '$lte': new Date('2015-07-05 23:59:59')}}} 

查詢結(jié)果如下(展示一種方式:其他展示略有不同): 

[ { _id: 5599b09bc16aac90e9fb7995, sendDate: '2015-07-05' },  { _id: 5599b161c16aac90e9fb7996, sendDate: '2015-07-05' },  { _id: 5599b161c16aac90e9fb7997, sendDate: '2015-07-05' } ]  

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤冈县| 鸡东县| 沙洋县| 弋阳县| 治县。| 定安县| 太仓市| 久治县| 天等县| 双辽市| 铜鼓县| 长宁县| 毕节市| 涞源县| 阿图什市| 沂水县| 高州市| 桐城市| 青阳县| 诸暨市| 鄂托克旗| 凤山市| 淄博市| 沙雅县| 措勤县| 涡阳县| 巴东县| 天津市| 淮阳县| 黑水县| 河北区| 攀枝花市| 云浮市| 临沭县| 新竹县| 廉江市| 基隆市| 泊头市| 无棣县| 驻马店市| 商水县|