Calculated group-by fields in MongoDB

Posted by Navin Viswanath on Stack Overflow See other posts from Stack Overflow or by Navin Viswanath
Published on 2014-08-22T00:11:48Z Indexed on 2014/08/22 4:20 UTC
Read the original article Hit count: 259

For this example from the MongoDB documentation, how do I write the query using MongoTemplate?

db.sales.aggregate(
   [
      {
        $group : {
           _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
           totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
           averageQuantity: { $avg: "$quantity" },
           count: { $sum: 1 }
        }
      }
   ]
)

Or in general, how do I group by a calculated field?

© Stack Overflow or respective owner

Related posts about java

Related posts about mongodb