Skip to content

Instantly share code, notes, and snippets.

@anapaulagomes
Created September 8, 2015 03:44
Show Gist options
  • Save anapaulagomes/25a147c273af7ad0c4e7 to your computer and use it in GitHub Desktop.
Save anapaulagomes/25a147c273af7ad0c4e7 to your computer and use it in GitHub Desktop.
Calcula a média de um atributo de um vetor excluindo um atributo desejado - MongoDB
db.grades.aggregate([
{ $unwind : "$scores" },
{ $project : { type : "$scores.type" , score: "$scores.score", class_id: "$class_id"} },
{ $match : { type : { $ne : "atributo_que_quer_excluir" } } },
{ $group:
{
_id: '$class_id',
media: {$avg: '$score'}
}
},
{ $sort: {media:-1} }
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment