猫鼬排除字段

Mongoose exclude fields

如果没有 select 链接,使用 mongoose 是否可以在填充时排除字段?

在这种情况下,我需要省略作者电子邮件和散列密码:

.populate(ratings, { path: 'reviews.author', model: 'User' }, function(err, ratings) {
    ...    
});

在填充选项中使用 select 属性:.

例如,如果您的用户具有 nameemail 属性,您可以 select 他们像这样:

.populate(ratings, { path: 'reviews.author', model: 'User', select: 'name email' }, function(err, ratings) {
    ...    
});

试试下面的方法;

Model.find(...)
     .populate({
          path: 'reviews.author',
          model: 'User',
          select: '-email -password'
     })
     .exec(callback);

这将排除 emailpassword 字段。