Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

421
Visualizações
How to select, groupBy, and join in Waterline or MongoDB

I have three models: user, noun, and usernoun (user_noun in PHP/Eloquent). There is a many to many relationship between user and noun. The "pivot" table has an extra attribute score. I can use this query with Eloquent to sum the scores of each noun a user has to get the user's total score:

$users = User::leftJoin('noun_user', 'user.id', 'noun_user.user_id')
    ->groupBy('user.id')
    ->select('user.*', DB::raw('sum(noun_user.score) as score'))
    ->orderBy('score', 'desc')
    ->get();

But I can't figure out how to get this to work in Waterline. This works, but not when I uncomment the .populate('user') line. I need user to be populated.

UserNoun
    .find({})
    //.populate('user')
    .groupBy('user')
    .sum('score')
    .sort({ score: 'desc' })
    .exec((err, usernouns) => {
    return res.json(usernouns)
})

here is a .native() query that works:

UserNoun.native(function(err, collection) {
    collection.aggregate([
        {
            $lookup: {
                from: 'user',
                localField: 'user',
                foreignField: '_id',
                as: 'user'
            }
        },
        {
            $group: { _id: '$user', total: { $sum: '$score' } }
        },
        {
            $sort : { total: -1 }
        }
    ]).toArray(function (err, results) {
        return res.json(results)
    })
})

Can this native query be rewritten in Waterline with groupBy and populate and sum?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As of Waterline@0.11.8, the only way to do that is to use native queries.

By the way, you can export your method inside the User model:

// User.js
module.exports = {

    // ...

    findTotalScores: function (callback) {
        UserNoun.native(function(err, collection) {
            if (err) {
                return callback(err);
            }

            collection.aggregate([
                {
                    $lookup: {
                        from: 'user',
                        localField: 'user',
                        foreignField: '_id',
                        as: 'user'
                    }
                },
                {
                    $group: { _id: '$user', total: { $sum: '$score' } }
                },
                {
                    $sort : { total: -1 }
                }
            ]).toArray(callback);
        });
    }

};

And you can use it in your controller by calling:

User.findTotalScores(function (err, results) {
    return res.json(results);
});
over 4 years ago · Santiago Trujillo Relatório

0

You can not do joins with waterline as of now, you will have to use raw queries for that. populate will just populate the associated fields, it does not return you the result the way sql join returns it.

Waterline at this point only supports using groupBy in combination with sum(), count() etc.

For groupby and sort you can use following :

Model.find()  
.groupBy('term') 
.sum('count')  
.limit(20)
.sort({count: 'desc'}) 
.exec(function (err, data){
//Your code here..
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda