0

I have an object structure as shown below

{
    "_id" : ObjectId("55d164f1c8f2c53a82535b9a"),
    "plant_name" : "TOTAL",
    "installed_capacity" : 3473,
    "wind_data" : [ 
        {
            "date" : "16-08-15",
            "timestamp" : " 16:27:15",
            "generated_capacity" : 617.24,
            "frequency" : 50.01
        }, 
        {
            "date" : "16-08-15",
            "timestamp" : " 21:21:15",
            "generated_capacity" : 670.25,
            "frequency" : 49.94
        }, ....]
}

I need to sum up (at least retrieve) "generated_capacity" of all the objects under "wind_data" having "date" equal to "16-08-15" of "TOTAL" object. I have tried this query

   db.collectionName.aggregate(
      {"$unwind":"$wind_data"},
      {"$match":{"plant_name":"TOTAL","wind_data.date":"16-08-15"}}
      )

But, this query is not working. Please suggest some way to figure this out.

chaitu
  • 1,036
  • 5
  • 20
  • 39
  • You are missing a closing quote, and you really should also `$match` first as well, but otherwise this should be working up to this point. What is wrong? – Blakes Seven Aug 17 '15 at 06:51
  • It returns all the fields of the object. How to find the sum of particular field inside the array. Like "generated_capacity" inside the array. – chaitu Aug 17 '15 at 06:56
  • add this after match `{"$group":{"_id":"$wind_data.date","generated_capacity_sum":{"$sum":"$generated_capacity}}}` – Neo-coder Aug 17 '15 at 06:58
  • This is a very basic operation. You should spend some time reading [SQL to Aggregation Mapping Chart](http://docs.mongodb.org/v3.0/reference/sql-aggregation-comparison/) which includes things like this as common examples. – Blakes Seven Aug 17 '15 at 07:01
  • Thanks @BlakesSeven . i am two days old to this querying in mongodb. Couldnt find any useful material. Anyway thanks for sharing. Will build upon this – chaitu Aug 17 '15 at 07:06
  • @Yogesh: Thanks for the help !!! It is returning the sum, but i have no clue why the sum is zero (even though i have numbers) – chaitu Aug 17 '15 at 07:11
  • Most of the [Aggregation Operators](http://docs.mongodb.org/manual/reference/operator/aggregation/) are reasonably well documented with examples, which should be enough to get you started. There are also the tests on github within the main repository as well as various driver tests in their own repository. – Blakes Seven Aug 17 '15 at 07:11
  • possible duplicate of [MongoDB 2.1 Aggregate Framework Sum of Array Elements matching a name](http://stackoverflow.com/questions/12162681/mongodb-2-1-aggregate-framework-sum-of-array-elements-matching-a-name) – Blakes Seven Aug 17 '15 at 07:12
  • What @Yogesh wrote is incorrect because you did not reference the parent `$wind_data.generated_capacity` instead. Just like you are doing elsewhere – Blakes Seven Aug 17 '15 at 07:13
  • @BlakesSeven: Thanks for the material shared. Your suggestion is working !!! – chaitu Aug 17 '15 at 07:18

1 Answers1

0

The following query would do the job

    db.collectionName.aggregate([      
      {"$unwind":"$wind_data"},       
      {"$match":{"plant_name":"TOTAL","wind_data.date":"16-08-15"}},
      {"$group":{"_id":"$wind_data.date","generated_capacity_sum":{"$sum":"$wind_data.generated_capacity"}}}
    ])
Arun K
  • 242
  • 1
  • 5