sails.js水线保存,读取,分析列表或数组

在Sails Waterline中创build,阅读和分析列表的正确方法是什么?

我一直无法find堆栈溢出或水线Github上的任何东西。

准确地说,我想保存一个想法模型中的标签列表,然后能够通过标签进行search。

理念模式:

attributes: { tags: { type: 'string' } } 

创build函数(标记以逗号分隔的string传递)

 create: function (req, res, next) { tags: req.param('tags').split(','); Idea.create(tags, function ideaCreate(err,idea) { //do stuff after create }); }, 

这成功地存储标签:即标签= ['tag1','tag2',…,'tagN']

我已经尝试过.find 。的时髦组合,但无济于事。

简易版本:我如何返回有一些用户请求tagX

更难的版本:如何返回任何有一个标签列表中至less有一个标签的想法?

选项一

我build议你为你的标签 创build一个模型

 attributes: { ideas: { collection: 'idea' }, name: { type: 'string' } } 

接下来,编辑您的意见模型来引用您的标记模型

 attributes: { tags: { collection: 'tag' }, name: { type: 'string' } } 

然后为了得到与“tagX”相关的所有想法,你可以这样做:

 var tag = "tagX"; // This same code should also work with an array, // but then you will have to use find instead of findOne // var tag = ["tagX", "tagY", "tagZ"] Tag.findOne({name: tag}).populate('ideas').then(function (tag) { // Do anything you want with the Ideas. tag.ideas.forEach(function(idea) { console.log(idea); }); }).catch(console.err); 

使用标签'tagX'和'tagY'创build想法'一些好主意'在标签中添加标签和从标签中删除标签非常简单。

 Promise.all([Idea.create({ name: 'Some Grand Idea'}), Tag.create({ name: 'TagX'}), Tag.create({ name: 'TagY'})]). spread(function (idea, tagX, tagY) { // Add tagX idea.tags.add(tagX.id); // Add tagY idea.tags.add(tagY.id); // To remove a tag, simply call remove // idea.tags.remove(1) idea.save(console.log); }).catch(console.log); 

所以总之,得到一个Idea模型。 并向Idea.tags集合添加/删除标签模型。 这两种方式,即你可以得到一个标签模型,并添加一个想法到Tag.ideas集合tag.ideas.add(someIdea.id) ,它的工作原理是一样的。

选项二

或者,按照您设置的方式使用Idea模型。

用一些标签获取想法:

 Idea.find({ tags: { 'like': '%tagX%' }}) 

通过一系列标签获取创意:

 Idea.find({ or : [ { tags: { 'like': '%\'tagX\'%' } }, { tags: { 'like': '%\'tagY\'%' } }, { tags: { 'like': '%\'tagZ\'%' } } ] })