Javascript Sails.js通过外键查询数据库

Javascript Sails.js通过外键查询数据库,javascript,sails.js,waterline,Javascript,Sails.js,Waterline,我想知道如何使用默认的水线模型通过外键进行查询 我有两个模型的职位和类别-职位有一个外键类。我需要这样做一个查询: Post.find({ where: { category: query } }).exec(function (err, data) {}); 在本例中,查询是一个字符串,因此返回的结果应该是包含搜索类别的帖子 最好的方法是什么 注意:当前示例不起作用说明了如何使用类别id实现此功能: Category.find().where({ name: query })

我想知道如何使用默认的水线模型通过外键进行查询

我有两个模型的职位和类别-职位有一个外键类。我需要这样做一个查询:

Post.find({
  where: {
    category: query
  }
}).exec(function (err, data) {});
在本例中,查询是一个字符串,因此返回的结果应该是包含搜索类别的帖子

最好的方法是什么


注意:当前示例不起作用

说明了如何使用类别id实现此功能:

Category.find().where({ name: query }).exec(function (error, categories) {
   var catArr = [];

   if (categories.length) {
     categories.map(function (item) {
       catArr.push(item.id);
     });
   }

   Post.find().where({ category: catArr }).exec(function (error, posts) {
     // do stuff
   });

});
还必须在模型中添加如下属性:

// Post
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    category: {
       model: 'category'
    }
  }
};

// Category
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    post: {
       model: 'post'
    }
  }
};

已了解如何使用类别id实现此功能:

Category.find().where({ name: query }).exec(function (error, categories) {
   var catArr = [];

   if (categories.length) {
     categories.map(function (item) {
       catArr.push(item.id);
     });
   }

   Post.find().where({ category: catArr }).exec(function (error, posts) {
     // do stuff
   });

});
还必须在模型中添加如下属性:

// Post
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    category: {
       model: 'category'
    }
  }
};

// Category
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    post: {
       model: 'post'
    }
  }
};
你的模型应该是

// Post
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    category: {
       model: 'category'
    }
  }
};

// Category
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    post: {
       collection: 'Post',
       via: 'category'
    }
  }
};
然后,将创建来自类别的查询

Category
    .find()
    .where({ name: query })
    .populateAll()
    .exec(function (error, categories) {
       var catArr = [];

       if (categories.length) {
         categories.map(function (item) {
           catArr.push(item.id);
         });
       }

       Post.find().where({ category: catArr }).exec(function (error, posts) {
         // do stuff
       });

    });
或者你可以通过post查询

如果您想从post查询categoryId,请确保您知道它。我通常使用categoryId是string,slugify from name,所以我可以通过名称查询类别,并确保类别名称和ID当然是唯一的。

您的模型应该是

// Post
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    category: {
       model: 'category'
    }
  }
};

// Category
module.exports = {
  attributes: {
    name: {
       type: 'string'
    },
    post: {
       collection: 'Post',
       via: 'category'
    }
  }
};
然后,将创建来自类别的查询

Category
    .find()
    .where({ name: query })
    .populateAll()
    .exec(function (error, categories) {
       var catArr = [];

       if (categories.length) {
         categories.map(function (item) {
           catArr.push(item.id);
         });
       }

       Post.find().where({ category: catArr }).exec(function (error, posts) {
         // do stuff
       });

    });
或者你可以通过post查询


如果您想从post查询categoryId,请确保您知道它。我通常使用categoryId是字符串,从名称开始使用slugify,因此,我可以通过名称查询类别,并确保类别名称和ID当然是唯一的。

很好的解决方案,但我在查询帖子时有额外的参数,不知道类别ID,所以我使用回答中提到的解决方案-还因为帖子类别只有一个关联,所以我只使用模型:根据文档,在我的模型中使用“category”/model:“post”。这是可以实现的,但在我给出解决方案之前,为什么post属性中不包含category?因为NoSQL中的一对一关系实际上更好地服务于它的主要领域。枚举可以通过枚举实现。我知道这一点,但类别应该由用户动态插入/修改。这就是为什么enum不是一个选项。如果你看看我的解决方案,你会注意到类别作为一个属性包含在帖子模型中。一个类别下有很多帖子是有意义的。我很好奇为什么你会在类别和帖子之间建立一对一的关系?这是一个很好的解决方案,但我在查询帖子时有额外的参数,并且不知道类别id,所以我使用的是我的答案中提到的解决方案——也因为帖子类别只有一个关联,所以我在我的文章中只使用了模型:'category'/模型:'post'根据文档进行建模。这是可以实现的,但在我给出解决方案之前,为什么类别不包括在您的帖子属性中?因为NoSQL中的一对一关系实际上更好地服务于它的主要领域。枚举可以通过枚举实现。我知道这一点,但类别应该由用户动态插入/修改。这就是为什么enum不是一个选项。如果你看看我的解决方案,你会注意到类别作为一个属性包含在帖子模型中。一个类别下有很多帖子是有意义的。我很好奇你为什么要在分类和帖子之间建立一对一的关系?