在这种情况下如何填充猫鼬? [英] How to populate in this case Mongoose?

查看:74
本文介绍了在这种情况下如何填充猫鼬?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const userSchema = new mongoose.Schema({
      email: { type: String, unique: true },
      password: String,
      notifications: [{
        notifiId: {type: Schema.Types.ObjectId, ref: 'Notifications' },
        viewed: { type: Number, default: 0 }
      }],
    }));

如何将notifiId填充到通知中?

我的代码:

User
    .findOne({_id: userNotifications.userId})
    .populate({path:'notifications', match: {notificationType: 
    userNotifications.notificationType}, populate: {path: notifiId })
    .exec(function(err, user){
      if(err || !user){
        return callback(1, null);
      }
      return callback(null, user.notifications)
    });

推荐答案

尝试:

  notifications: {
    notifiId: {type: Schema.Types.ObjectId, ref: 'Notifications' },
    viewed: { type: Number, default: 0 }
  },

并找到它:

model_Variable.findById(req.params.id).populate("notifiId").exec(function (err, 
callBack) {});

这篇关于在这种情况下如何填充猫鼬?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆