0

In mongodb I am trying to fetch the count of month from a collection which holds the field in the format of ISO Date..

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

retrived the date format as

2015-Jun-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..

I just need count of month from a collection.

colud some one suggest some ideas for this issue

kothai
  • 17
  • 8
  • possible duplicate of [Find objects between two dates MongoDB](http://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) – Blakes Seven Jul 28 '15 at 05:27
  • What did you tried so far? – Gunaseelan Jul 28 '15 at 05:29
  • i just exploded the Date and retrived the month separetly struggling to put mongodb query to obtain the count of month. – kothai Jul 28 '15 at 05:32
  • am not trying to fetch the objects between two dates am just trying to fetch the count of month from january to december. – kothai Jul 28 '15 at 05:34

1 Answers1

0

As I understand you want to get month count without duplications, so you can use $group of aggregation framework.

For examle if in your collection name of ISODate field is date, the query can be something like this:

db.COLLECTION_NAME.aggregate(
   [
      {
        $group : {
           _id : { month: { $month: "$date" } },
           monthCount: { $sum: 1 }
        }
      }
   ]
)
Taron Saribekyan
  • 1,360
  • 7
  • 13
  • Thankyou for your valuable response..How do i fetch the records from collection by monthwise using "PHP with mongodb"..could you suggest me some example query..that would be great helpful for me. – kothai Jul 28 '15 at 06:15
  • @kothai Sorry, but I not understand your question, what means "monthwise" ? – Taron Saribekyan Jul 28 '15 at 06:18
  • @kothai Please see this http://docs.mongodb.org/manual/reference/operator/aggregation/group/#group-by-month-day-and-year – Taron Saribekyan Jul 28 '15 at 06:42
  • Thank you for the documentation .. It works!!!..I just implemented it in putty which shows the correct output...how do i implement it with "PHP and mongodb". how do i reformat the shell command for my " PHP with MongoDB".?? – kothai Jul 28 '15 at 07:26
  • @kothai You need to install PHP driver for Mongo: https://github.com/mongodb/mongo-php-driver#installing-on-windows And than learn how to use. official PHP tutorial: http://php.net/manual/en/mongo.tutorial.php other tutorials: http://www.sitepoint.com/building-simple-blog-app-mongodb-php/ http://www.tutorialspoint.com/mongodb/mongodb_php.htm – Taron Saribekyan Jul 29 '15 at 18:30