我刚开始使用 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/