user
, noun
및 usernoun
(PHP/Eloquent에서는 user_noun
)의 세 가지 모델이 있습니다. user
과 noun
사이에 많은 관계가 있습니다. "피벗"테이블에는 추가 속성 score
이 있습니다. 나는 사용자가 사용자의 총 점수를 얻을 수있는 각 명사의 점수 합계를 웅변으로이 쿼리를 사용할 수 있습니다워터 라인 또는 MongoDB에서 선택, 그룹화 및 가입하는 방법
$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();
하지만이 워터에서 작동하도록하는 방법을 알아낼 수 없습니다. 이 기능은 작동하지만, .populate('user')
행의 주석을 제거하지 않은 경우에는 작동하지 않습니다. 입력하려면 user
이 필요합니다. 여기
UserNoun
.find({})
//.populate('user')
.groupBy('user')
.sum('score')
.sort({ score: 'desc' })
.exec((err, usernouns) => {
return res.json(usernouns)
})
는 작동하는
.native()
쿼리
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)
})
})
이 기본 쿼리가 groupBy
및 populate
및 sum
와 워터에 다시 쓸 수 있습니다?
당신은'에 대한 참조 링크를 공유시겠습니까 groupBy'와'sum' 메쏘드? – Sangharsh
정확한 문서를 찾을 수 없지만 sails의 groupby가 계산 기능을 사용하여 쿼리를 시도했습니다. 하나의 링크 발견 - http://stackoverflow.com/questions/20254910/how-do-i-perform-this-query-using-sailsjs-orm-waterline –