gpt4 book ai didi

node.js - 如何只查询模型的某个字段?

转载 作者:搜寻专家 更新时间:2023-11-01 00:17:03 24 4
gpt4 key购买 nike

我有这样一个模型:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com