作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有这样一个模型:
module.exports = {
attributes: {
email: {
type: 'email',
required: true,
unique: true
},
password: {
type: 'string',
minLength: 6,
required: true
},
articles: {
collection: ‘article',
via: 'owners'
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
}
}
如何在toJSON函数中只查询'email'字段而不删除其他字段?
这个问题的原因是,如果我有成千上万篇带有嵌入图像等的文章,收集所有这些文章并在 toJSON 函数中删除它们,只是为了显示“电子邮件”,那将是一种浪费仅字段。
因此,必须有更好的方法来实现这一点。我希望你们能回答我这个问题:)
更新我忘了说我尽可能多地使用 blueprint.js 以避免覆盖 Controller 中的创建、删除、更新...。
最佳答案
它没有很好的文档记录,但 Waterline 提供了一个 select criteria在这里实现https://github.com/balderdashy/waterline-criteria/blob/master/lib/projections/select.js
我测试了以下内容并且有效。 {}
可以是任何查询:
Model.find({}, {select: ['email']}).exec(function(err, result) {
return res.send(result);
});
关于node.js - 如何只查询模型的某个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39365168/
我是一名优秀的程序员,十分优秀!