【发布时间】:2018-06-17 04:30:34
【问题描述】:
我的数据库中有一个名为 businesses 的集合。我想要做的是查询在我的架构中定义的数据库和特定字段,而不是文档的所有字段。我认为这就是架构首先存在的原因?
架构
var businessSchema = new mongoose.Schema({
custom_id: String,
name: String
});
module.exports = mongoose.model('Business', businessSchema);
快递
router.get('/query', function (req, res, next) {
res.type('json');
Business.find({custom_id: req.query.custom_id})
.then(function (data) {
res.send({data: data});
}).catch(function (err) {
return next(new Error(err.message || err));
})
});
回应
{
"data":[
{
"_id":"5a50ac105a0d8452b0e341e5",
"custom_id":"1",
"name":"Dave and Jane",
"status":"active",
"verified":true,
"created":1492727550760,
"email":{
"address":"dave_jane@whatever.com"
}
}
]
}
在架构中,我只有 custom_id 和 name,但无论我定义什么字段(或者我没有定义),当Business.find 执行时,文档的所有字段都会返回。
与架构为空的行为相同,因此返回所有字段。
【问题讨论】:
标签: node.js mongodb mongoose mongoose-schema