如何计算水线/帆船的关联大小? [英] How to count association size with waterline/sails?
问题描述
使用帆0.10.5/水线0.10.15:
Using sails 0.10.5/waterline 0.10.15:
我找不到一个简单问题的答案:如何在不使用populate()(将加载所有数据的情况下)的情况下计算关联的元素.
I cannot find an answer to a simple question: how to count the elements of an association without using populate() (which would load all data).
让我们通过via建立一个简单的many2many关系:
Let take a simple many2many relation with via:
User:
attributes: {
following: {
collection: 'user',
via: 'follower',
dominant: true
},
follower: {
collection: 'user',
via: 'following'
}
现在我需要集合的大小. 目前我正在尝试
Now I need the size of the collections. Currently I try
User.findById(1).populateAll().exec(function(err, user) {
// count of followings -> user.following.length;
// count of followers-> user.follower.length;
}
这会导致加载收藏集.
- 我在收集级别缺少计数功能,以避免填充/加载数据.
- 是否可以访问(自动生成的)联接表以直接在联接上运行计数查询?
类似的东西:
User.findById(1).count({'followings'}).exec(function(err, followings) {
...}
或
UserFollowingFollow_FollowFollowing.countByUserFollowingFollowId(1).
exec(function(err, followings) {
...}
推荐答案
Waterline确实提供了count
查询方法,可以像这样使用它来解决您的问题:
Waterline does offer the count
query method and it can be used like this to solve your problem:
User.count().where({follower: followerId})
.exec(function(err, numberOfFollowings) {
//numberOfFollowings will be the integer that you need
})
followerId
是您在示例中传递给User.findOne()
的ID.
followerId
is the id that you are passing to User.findOne()
in your example.
您还可以阅读水线文档关于这一点.
You can also read the Waterline documentation about this.
这篇关于如何计算水线/帆船的关联大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!