【问题标题】:Sails.js / Waterline: How do I use a collection attribute in a find query?Sails.js / Waterline:如何在查找查询中使用集合属性?
【发布时间】:2015-12-06 22:13:34
【问题描述】:

我正在处理按集合属性过滤对象的 Waterline 查询。在这个简化的例子中,我有两个模型,VideoCategory

// Video.js

module.exports = {

attributes: {
    title: {
      type: 'string'
    },
    categories: {
      collection: 'Category',
      via: 'videos'
    }
  }
};


// Category.js

module.exports = {

  attributes: {
    name: {
      type: 'string'
    },
    videos: {
      collection: 'Video',
      via 'categories'
    }
  }
};

我想查找与某个类别相关的所有视频。我将类别 ID 存储在一个名为 categoryID 的变量中,并且正在尝试此查询:

Video.find('categories': categoryID).exec(function (err, videos) {
    // videos should contain all videos associated with the categoryID
});

但是,即使有与我正在查找的类别相关联的视频,我也总是得到一个空结果。我知道 waterline 目前不支持对集合属性中的值进行深度查询,但我认为至少查询对象的 id 是可行的。我错了吗?

如果是这样,是否有其他方法可以在不使用本机查询的情况下实现所需结果?

我知道我可以向Category 添加一个集合属性,并从Category 端构建我的查询。然而,这只是更复杂搜索的开始,我还使用存储在Video 对象中的其他属性来缩小结果范围,例如视频创建者的用户 ID。最后,我使用分页遍历视频结果。因此,我正在寻找一种方法来检索特定类别的视频,该视频可以与存储在 Video 对象中的其他搜索属性相结合。

【问题讨论】:

    标签: node.js sails.js waterline


    【解决方案1】:

    修改Category.js为:

    module.exports = {
    
      attributes: {
        name: {
          type: 'string'
        },
        videos: {
          collection: 'Video',
          via: 'categories'
        }
      }
    };
    

    修改Video.js

    module.exports = {
    
      attributes: {
        title: {
          type: 'string'
        },
        categories: {
          collection: 'Category',
          via: 'videos'
        }
      }
    };
    

    添加视频时,

    var title = req.param('title');
    var categories = req.param('categories').split(','); //ids of categories in format 1,3,8 etc
    Video.create({name: title, categories: categories}, function(err, succ){
        if(err){
            return res.serverError(err);
        }else {
            return res.json(200, succ);
        }       
    });
    

    要查找具有特定类别的所有视频,请在水线中使用 populate() 助手。

    var categoryId = req.param('catId');
    Category.find({id: categoryId}).populate('videos').exec(function(err, results){
        if(err){
            return res.serverError(err);
        }else {
            return res.json(200, results);
        }
    });
    

    【讨论】:

    • 感谢您的建议。不幸的是,我认为它不适用于我的情况。我知道我可以从类别方面开始查询。然而,问题是这只是一个更复杂的查询的开始。视频还具有其他属性,例如对创建它的用户的引用。现在,如果我将我的查询与与视频对象关联的其他参数结合起来,我就可以从类别端开始搜索并填充视频属性。任何想法如何解决这个问题?
    【解决方案2】:

    视频模型:

      module.exports = {
        attributes: {
         name: {
                type: 'string'
         },
         categories: {
                collection: 'category',
                 via: 'videos'
         },
          toJSON: function() {
               var obj = this.toObject();
               return obj;
          }
       }
    };
    

    类别模型:

    module.exports = {
    
        attributes: {
              name: {
                   type: 'string'
              },
       videos: {
           collection: 'Video',
           via: 'categories'
       }
    }
     };
    

    您的查询将如下所示:

     var arr = ['56667a2cbaea1fcd11c54851','56667b1053c6c37a1283ea75'];
    
     Video.find().populate("categories",{id:arr}).exec(function(e, r) {
            res.json(r);    
    })
    

    【讨论】:

    • 感谢 rroxysam,这与 MjZac 建议的方法基本相同。不幸的是,它在我的情况下不起作用,因为这只是一个更复杂查询的开始,我使用除了与视频对象关联的类别之外的其他属性来缩小结果范围。任何想法如何解决这个问题?
    • 将视频链接到类别时,只需将类别id保存在类别字段中,然后使用上述查询即可获取链接到指定类别的所有视频数据。如果仍然,请告诉我。 * 见上面修改后的代码
    • 感谢您的更新。如果我每个视频只有一个类别,这将起作用。不幸的是,在我的情况下,一个视频可以有多个类别,所以我不能将Video 中的category 属性更改为model 类型,它必须是collection。任何想法如何使它与集合属性一起工作?
    • 您好 Andi,问题是您没有以正确的方式关联/保存多对多关联。当您在sails js 中创建多对多关联时,相互关联的id 的单独集合。是由风帆创造的。前任。如果您有视频和类别集合,那么将有一个集合 video_categories__category_videos 映射相互关联的 id,然后您将能够获得所需的输出,其中类别和视频是通过模型中的属性。 [参考] (sailsjs.org/documentation/concepts/models-and-orm/associations/…)
    • 您好 rroxysam,非常感谢您一直以来的帮助!我以前试过这个,但这并没有改变结果。我再次对其进行了更新,并更改了我的问题以向您展示编辑内容。尽管可以在“localDiskDb.db”的“video_categories__category_videos”中找到相应的条目,但这仍然给我一个空的结果。当您尝试时,这对您有用吗?任何其他想法我做错了什么?
    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 2015-08-24
    • 2015-08-25
    • 1970-01-01
    相关资源
    最近更新 更多