Skip to content

Instantly share code, notes, and snippets.

View anapaulagomes's full-sized avatar

Ana Paula Gomes anapaulagomes

View GitHub Profile
@anapaulagomes
anapaulagomes / gist:18be9028e92b5033c0b4
Last active December 29, 2020 15:37
Agrupa e calcula a média com aggregate - MongoDB
db.zips.aggregate([
{
$match: {
$and : [
{ $or: [ { state: 'XX' }, { state: 'YY' } ] }, { pop: {$gte: 25000}}
]}
},
{
$group:
{
@anapaulagomes
anapaulagomes / gist:138958fceb012c9616cb
Last active December 19, 2016 16:59
Group array attributes, sum then and order from the biggest to the smallest - MongoDB
var pipeline = [{$unwind: '$comments'}, {$group: {_id: '$comments.author', numberOf: {$sum: 1}}}, {$sort: {numberOf:-1}}]
db.posts.aggregate(pipeline)
@anapaulagomes
anapaulagomes / gist:b5d6c5b6d06584c9a774
Created September 8, 2015 02:55
Cria um vetor com os atributos encontrados e soma-os - MongoDB
var pipeline = [
{ $group: {
_id:{'members': '$member.name'},
events:{$addToSet: '$event.name'},
number_events:{$sum:1}}
},
{$sort: {number_events:-1} }
]
db.rsvps.aggregate(pipeline)
@anapaulagomes
anapaulagomes / gist:e6ce97efad0b2cb49fc6
Last active November 22, 2016 18:53
Count unique records according to parameters - MongoDB
db.checkins.distinct('name', {city: 'london', class: 'resident'}).length;
@anapaulagomes
anapaulagomes / gist:7083e1d292e6c9d47308
Created September 8, 2015 02:58
Agrupa de acordo com parâmetros de busca e soma de acordo com um parâmetro da coleção - MongoDB
db.checkins.aggregate([
{ $match :
{
nome: 'AKB48劇場 (AKB48 Theater)',
classe: 'turista' }
},
{$group:
{
_id: '$horario',
checkins: {$sum: 1}}
@anapaulagomes
anapaulagomes / gist:25a147c273af7ad0c4e7
Created September 8, 2015 03:44
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'}
}
@anapaulagomes
anapaulagomes / gist:377caf9fd44554a3c5a7
Created September 15, 2015 15:12
Atualiza vários registros de um campo com base em uma consulta
db.collection.update({'base.classe': 'turist'}, {$set: {'base.classe': 'turista'}}, {multi: true})
@anapaulagomes
anapaulagomes / gist:cccabef3fe44a49b37252401c7df56e7
Created May 9, 2016 17:50
Busca com o uso da cláusula OU (lista de valores desejados)
db.checkins.find({'base.nome': 'copa2014', 'base.cidade': { $in: ['london', 'tokyo', 'rio', 'newyork']}})
db.checkins.aggregate(
{ "$match": {"base.cidade": "newyork", "local.nome": "Starbucks"}},
{ "$project": { "h":{"$hour":"$data"} } },
{ "$group":{ "_id": { "hour":"$h"}, "total":{ "$sum": 1} } }, {$sort: {"_id.hour": 1}})
@anapaulagomes
anapaulagomes / gist:53db7ec2f3d31f5c9fb613ca1c737544
Created May 26, 2016 18:28
Filtra por finais de semana e agrupa por horas
db.checkins.aggregate([
{'$project':{
'dia_da_semana': {'$dayOfWeek': '$data'},
'usuario.cidades.residencia.pais': 1,
'h':{'$hour':'$data'},
'base.cidade': 1,
'base.nome': 1,
'base.classe': 1
}
},