在猫鼬中填充子文档
populate subdocument in mongoose
我希望有人能帮我解决这个问题。我正在尝试填充一个 subSchema,但我没有运气。
我有一个 replySchema
、commentSchema
和一个 UserSchema
。
replySchema
是 commentSchema
的子文档,我想用 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;
USER-SCHEMA
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;
非常感谢您的宝贵时间!
在回复中填充用户数据:
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'
}
}
]);
我希望有人能帮我解决这个问题。我正在尝试填充一个 subSchema,但我没有运气。
我有一个 replySchema
、commentSchema
和一个 UserSchema
。
replySchema
是 commentSchema
的子文档,我想用 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;
USER-SCHEMA
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;
非常感谢您的宝贵时间!
在回复中填充用户数据:
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'
}
}
]);