-
Notifications
You must be signed in to change notification settings - Fork 5
Queries MongoDB
Gabriel Tibúrcio edited this page Sep 26, 2017
·
8 revisions
Aqui serão documentadas queries interessantes do MongoDB para uso no sistema e referências futuras.
db.getCollection('submissoes').aggregate([
{
$group: {
_id: "$porcentagemAcerto",
quantidade: { $sum: 1 }
}
}
])
db.getCollection('submissoes').aggregate([
{ $match: { user: ObjectId("5991e8a50be9d42f2f68b988") } },
{ $group: {
_id: '$questao',
submissoes: { $push: '$$CURRENT' },
user: { $first: '$user' },
acerto: { $max: '$$CURRENT.porcentagemAcerto' }
}}
]);
db.getCollection('submissoesProva').aggregate([
{ $match: { prova: prova._id } },
{ $lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user'
}},
{ $unwind: '$user' },
{ $group: {
_id: '$user._id',
submissoes: { $push: '$$CURRENT' },
user: { $push: '$user' }
} },
{ $unwind: '$user' }
]);
Identificando questões de uma determinada lista pelo enunciado começando com [LX
onde X é o número da lista
db.getCollection('questoes').find({
enunciado: { $regex: /^\[L2/m }
})
db.getCollection('questoes').find({
enunciado: { $regex: /^\[L2/m }
}).forEach(function(doc) {
doc.enunciado = doc.enunciado.replace('\r\n', ' ')
db.questoes.update({ _id: doc._id }, { $set: { enunciado: doc.enunciado }})
})
db.getCollection('submissoes').aggregate([
{
$lookup: {
from: 'questoes',
localField: 'questao',
foreignField: '_id',
as: 'questao'
}
},
{ $unwind: '$questao' },
{
$match: {
'questao.titulo': 'Um ponto em um retângulo'
}
},
{
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user'
}
},
{ $unwind: '$user' }
])