gpt4 book ai didi

JavaScript按日期查询MongoDB中的数据的要点示例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章JavaScript按日期查询MongoDB中的数据的要点示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

group by date 聚合查询日期 统计每天数据(信息量) 1  。

?
1
2
3
4
5
{
   "_id" : ObjectId("557ac1e2153c43c320393d9d"),
   "msgType" : "text",
   "sendTime" : ISODate("2015-06-12T11:26:26.000Z")
}

  2  。

?
1
2
3
4
5
{
   "_id" : ObjectId("557ac1ee153c43c320393d9e"),
   "msgType" : "text",
   "sendTime" : ISODate("2015-06-12T11:26:38.000Z")
}

  3  。

?
1
2
3
4
5
{
   "_id" : ObjectId("557ac2012de5d32d213963b5"),
   "msgType" : "text",
   "sendTime" : ISODate("2015-06-12T11:26:56.000Z")
}

  4   。

?
1
2
3
4
5
{
   "_id" : ObjectId("557ac978bb31196e21d23868"),
   "msgType" : "text",
   "sendTime" : ISODate("2015-06-12T11:58:47.000Z")
}

  5  。

?
1
2
3
4
5
{
   "_id" : ObjectId("557ac9afbb31196e21d23869"),
   "msgType" : "text",
   "sendTime" : ISODate("2015-06-12T11:59:43.000Z")
}

     SQL Here   。

?
1
2
3
4
5
6
7
db.getCollection( 'wechat_message' ).aggregate(
   [  
     {  $project : { day : {$substr: [ "$sendTime" , 0, 10] }}},    
     {  $ group  : { _id : "$day" , number : { $ sum : 1 }}},
     {  $sort  : { _id : -1 }}    
   ]
)

     Result Here  。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"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种方法,根据日期查询 mongodb的查询真让人难以琢磨,就查询单天信息,都需要花费一番功夫才行。    第一种方式:   。

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

     第二种方式(第一种的变异):   。

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

  第三中方式(第二种的变异):  。

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

查询结果如下(展示一种方式:其他展示略有不同):  。

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

最后此篇关于JavaScript按日期查询MongoDB中的数据的要点示例的文章就讲到这里了,如果你想了解更多关于JavaScript按日期查询MongoDB中的数据的要点示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com