node.js - 使用模型作为条件在 Waterline 中查询

标签 node.js mongodb model sails.js waterline

我刚开始使用 Waterline,我遇到了关于使用模型作为标准在数据库 Mongo 中搜索记录的问题。经过几个小时的搜索,我找不到任何满意的解决方案。

首先,我基本上有 2 个相互关联的模型:

Post.js

var Post = Waterline.Collection.extend({
tableName: 'Post',
connection: 'default',
attributes: {
    url : { type: 'string', required: true, unique: true, lowercase: true },
    title : { type: 'string', required: true },
    body : { type: 'string', required: true },
    author : { type: 'string', required: true },
    writeIn : { type: 'string', required: true },
    tags: {
        collection: 'Tag',
        via: 'posts',
        dominant: true
    },
    category: {
        model: 'Category'
    }
}});

分类.js

var Category = Waterline.Collection.extend({
tableName: 'Category',
connection: 'default',
attributes: {
    url: { type: 'string', required: true, unique: true, lowercase: true },
    name: { type: 'string', required: true },
    posts: {
        collection: 'Post',
        via: 'category'
    }
}});

它们使用多对多关联关联。关键是我想通过类别名称查询帖子列表。 像这样:

Post.find().where({category: {url: 'java'}})

你们知道怎么做吗?

最佳答案

是的,你可以通过不同的方式做到这一点......

Category.find()
     .where({url: 'java'})
        .populate('posts')

关于node.js - 使用模型作为条件在 Waterline 中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28638736/

相关文章:

javascript - Babel JS——简单地将 JavaScript STRING 转换为 es5(使用 nodejs)

javascript - react 导入语句

javascript - NodeJS MongoDB 连接字符串问题

asp.net-mvc - MVC最佳实践: Where to do mapping between models

node.js - 什么可能导致与 npm 无法找到文件相关的错误?我的 node_modules 子文件夹中没有内容。这是为什么?

javascript - 使用 Redis 扩展 Socket.IO,然后扩展 Redis 本身

MongoDB 查询没有返回结果

node.js - findAndModify mongodb 中的错误 - nodejs - 错误代码 17287

model-view-controller - 如何更改模型属性上的 "get"和 "set"?

validation - 验证集是训练集的一部分吗?