以下是mongo查询
db.booking.aggregate(
{$project:{
date: {$dateToString: { format: "%Y-%m-%d", date: "$dispatchDate"}},officeId: "$fromBranchId",total:"$totalCharge"
}},
{$group: {
_id: { date: "$date", officeId: "$officeId"},
total: {$sum: "$total"}
}},
{ $project: { bookings: { $concat: [ "$_id.date", ":",{ $convert: { input: "$total", to: "string" } } ]}}},
{$group: {
_id: {officeId: "$_id.officeId"},
bookings: { $addToSet: "$bookings" }
}}
)
等效的Java代码是
Aggregation aggregation = newAggregation(
match(Criteria.where(SessionManager.OPERATOR_ID).is(sessionManager.getOperatorId())),
project().and(DateOperators.dateOf("dispatchDate").toString("%Y-%m-%d")).as("date").and("fromBranchId").as("officeId").and("totalCharge").as("total"),group("date","officeId").sum("total").as("total"),project().andExpression("concat(date,':', substr(total,0,-1))").as("bookings"), group("officeId").addToSet("bookings").as("bookings"));
我在执行===> group("officeId").addToSet("bookings").as("bookings") is invalid reference id 'officeId'.
所以请帮助我如何在Java {$group: {_id: {officeId: "$_id.officeId"},bookings: { $addToSet: "$bookings" }}}
中执行这段代码
在此先感谢:)