如何计算水线/帆船的关联大小? [英] How to count association size with waterline/sails?

查看:96
本文介绍了如何计算水线/帆船的关联大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用帆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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆