【问题标题】:How to populate in this case Mongoose?在这种情况下如何填充 Mongoose?
【发布时间】:2018-12-15 06:15:28
【问题描述】:
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)
    });

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案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) {});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2022-01-23
      • 2015-11-30
      相关资源
      最近更新 更多