【问题标题】:Query in Waterline using a model as a criteria使用模型作为标准在 Waterline 中查询
【发布时间】:2015-02-20 21:43:08
【问题描述】:

我刚开始使用 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'
    }
}});

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'}})

你们中有人知道怎么做吗?

【问题讨论】:

    标签: node.js mongodb model sails.js waterline


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      相关资源
      最近更新 更多