【问题标题】:populate subdocument in mongoose在猫鼬中填充子文档
【发布时间】:2021-10-07 06:03:48
【问题描述】:

我希望有人可以帮助我解决这个问题。我正在尝试填充子架构,但我没有运气。 我有一个replySchemacommentSchema 和一个UserSchemareplySchemacommentSchema 的子文档,我想用来自 userSchema 的用户详细信息填充它们 我的问题是下面的代码没有填充我的replySchema

REPLY-SCHEMA 和 COMMENT-SCHEMA

const replySchema = new mongoose.Schema(
  {
    replyComment: {
      type: String,
      required: [true, 'Reply comment cannot be empty'],
    },
    createdAt: {
      type: Date,
      default: Date.now(),
    },
    user: {
      type: mongoose.Schema.ObjectId,
      ref: 'User',
      required: [true, 'Comment must belong to a user'],
    },
  },
  {
    timestamps: true,
  }
);

const commentSchema = new mongoose.Schema(
  {
    comment: {
      type: String,
      required: [true, 'Comment cannot be empty'],
    },
    createdAt: {
      type: Date,
      default: Date.now(),
    },
    // createdAt: Number,
    // updatedAt: Number,
    post: {
      type: mongoose.Schema.ObjectId,
      ref: 'Post',
      required: [true, 'Comment must belong to a Post'],
    },
    user: {
      type: mongoose.Schema.ObjectId,
      ref: 'User',
      required: [true, 'Comment must belong to a user'],
    },
    replies: [replySchema], //replySchema sub-document - is this the right way?
  },
  {
    // timestamps: { currentTime: () => Math.floor(Date.now() / 1000) },
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

replySchema.pre(/^find/, function (next) {
  this.populate({
    path: 'user',
    select: 'name role avatar',
  });
  next();
});

commentSchema.pre(/^find/, function (next) {
  this.populate({
    path: 'user',
    select: 'name role avatar',
  });
  next();
});

const Comment = mongoose.model('Comment', commentSchema);

module.exports = Comment;

用户模式

const userSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      trim: true,
      required: [true, 'Please insert your name'],
    },

    avatar: {
      type: Object,
    },

    role: {
      type: String,
      enum: ['user', 'admin'],
      default: 'user',
    },

  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

const User = mongoose.model('User', userSchema);

module.exports = User;

非常感谢您的宝贵时间!

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    回复中填充用户数据:

    this.populate([
        'user', // To populate commentSchema's user field
        {
            path: 'replies',
            populate: 'user'
        } // To populate replySchema's user field
    ]);
    

    编辑:

    填充特定字段:

    this.populate([
        {
            path: 'user',
            select: 'name role avatar'
        },
        {
            path: 'replies',
            populate: {
                path: 'user',
                select: 'name role avatar'
            }
        }
    ]);
    

    【讨论】:

    • 太棒了,它奏效了。谢谢!但是..你能告诉我如何在两个模式中填充选择特定字段,因为这将填充我的整个用户详细信息。在我选择字段之前,但它不适用于您的解决方案。谢谢
    • 我不确定它是否会起作用,但我编辑了我的回复,您可以尝试一下
    猜你喜欢
    • 2017-05-07
    • 2021-02-11
    • 2016-08-17
    • 2021-03-13
    • 2015-11-27
    • 2015-07-28
    • 2021-08-29
    • 2020-04-30
    • 2021-04-26
    相关资源
    最近更新 更多