简体   繁体   English

一个从MongoDB数据库中的集合中获取月份计数的PHP程序

[英]A PHP program to get the count of month from a collection in MongoDB database

In mongodb I am trying to fetch the count of month from a collection which holds the field in the format of ISO Date.. 在mongodb中,我试图从一个以ISO Date格式保存字段的集合中获取月份计数。

ISODate("2015-05-18T03:56:07.030Z") ISODate(“ 2015-05-18T03:56:07.030Z”)

retrived the date format as 检索日期格式为

2015-Jun-17 06:59:33 2015年6月17日06:59:33

From that , how do i get the count of month from (January to december from ISO Date) from the collection using mongodb query.. 从那,我如何使用mongodb查询从集合中获取月份(从ISO日期从1月到12月)。

I just need count of month from a collection. 我只需要从集合中计算月份数即可。

colud some one suggest some ideas for this issue 争论一些人对此问题提出一些想法

As I understand you want to get month count without duplications, so you can use $group of aggregation framework . 据我了解,您希望获得不重复的月份计数,因此可以使用$ group ofgregation framework

For examle if in your collection name of ISODate field is date , the query can be something like this: 例如,如果您在ISODate字段的集合名称中为date ,则查询可以是这样的:

db.COLLECTION_NAME.aggregate(
   [
      {
        $group : {
           _id : { month: { $month: "$date" } },
           monthCount: { $sum: 1 }
        }
      }
   ]
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM