在这种情况下如何填充猫鼬?

时间:2018-07-06 15:40:10

标签: javascript node.js mongodb mongoose

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)
    });

1 个答案:

答案 0 :(得分:1)

尝试:

  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) {});