Reputation: 8530
I want to exclude the '_id' field and replace it with 'action_id' field when using aggregate. The problem that i must specify all other fields in $project that i dont want to exclude. This is my query:
cursor = db.aggregate(
{
'$group': {
'_id': '$somefield',
'count': {'$sum': 1},
'average_latency': {'$avg': '$latency'}
}
},
{
'$project': {
'action_id': '$_id',
'_id': False,
'count': True,
'average_latency': True
}
}
What shall i do to avoid writing every 'field': True in $project?
Upvotes: 1
Views: 965
Reputation: 46471
You can use $addFields
to add an extra field and then $project
to remove _id
cursor = db.aggregate(
{ '$group': {
'_id': '$somefield',
'count': { '$sum': 1 },
'average_latency': { '$avg': '$latency' }
}},
{ '$addFields': { 'action_id': '$_id' }},
{ '$projects': { '_id': False }}
)
Upvotes: 1