我想通过id查询一个用户,然后只带一个类型匹配的食物,我该怎么做?mongoose FindbyId和数组匹配到一个字符串
var type = 'fish';
var id = '597a348286ffe50d882a3033';
User.findById(id, { 'food.type': type }, 'food', function(err, model) {
if (err) {
console.log(err);
}
if (!model) {
console.log('no model');
}
if (model) {
console.log(model);
}
});
文档的结构如何? – Dario
'VAR用户= mongoose.Schema({ \t \t 名:字符串, \t电话:字符串, \t电子邮件:字符串, \t食品:[{ \t 类型:字符串, 名:字符串, \t价格:字符串 \t \t}] } – John