Mongo DB component not working

I have the following data in a mongo collection (datacollection):

{
  "_id": ObjectId("54980efef7db7d9b018d375b"),
  "sub_id": "1234567890",
  "points": "10",
  "oid": "1",
  "name": "test",
  "app": "e53abc6d12fea63c80e4c2e1d49e",
  "source": "mysource",
  "product_name": "browser",
  "added": ISODate("2014-12-22T12:30:54.618Z"),
  "added_human": "2014-12-22 06:30:54",
  "checked": true
}

      

I really need to figure out the sum of the points grouped by date. The SQL query will look like this: SELECT DATE (added) as point_date, SUM (points) AS total_points FROM mytable WHERE added between 'from' AND 'and' GROUP BY DATE (added).

So, I am using the following aggregation:

db.datacollection.aggregate(
    { 
        $match:{
            "added" : {$gte:ISODate("2014-12-01"), $lt:ISODate("2014-12-25")},   
            "source":"mysource"  
        },   
        $group : {
            "_id" : { month: { $month: "$added" }, day: { $dayOfMonth: "$added" }, year: { $year: "$added" } },
            "total_points":{$sum:"$points"}   
        }  
    }  
);

      

But it shows

"errmsg" : "exception: A pipeline stage specification object must contain exactly one field.",

      

How can this be fixed or is there a better way to do this calculation?

+3


source to share


2 answers


use this:

db.datacollection.aggregate(
    { 
        $match:{
            "added" : {$gte:ISODate("2014-12-01"), $lt:ISODate("2014-12-25")},   
            "source":"mysource"  
        }
    },   
    {   $group : {
            "_id" : { month: { $month: "$added" }, day: { $dayOfMonth: "$added" }, year: { $year: "$added" } },
            "total_points":{$sum:"$points"}   
        }  
    }  
);

      

You must use this format (each pipeline is paired {}

)



db.collection.aggrgation ( 
   { $group: ....},
   { $match : .... },
   ...
})

      

but you used:

db.collection.aggrgation ( 
   { $group: ....,
     $match : ....,
     ...
   }
})

      

+10


source


Aggregate operand must be inside the group {}



0


source







All Articles