我在找到填充已填充字段的解决方案(未知的填充数量)时遇到了问题。
export const FilesSchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: true,
unique: true,
},
children: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Files',
},
],
},
{ timestamps: true },
);它表示可以是文件或文件夹的文件结构。一个文件夹可以将另一个文件夹和另一个文件夹与文件连在一起,填充的数量是未知的。对此有什么帮助吗?
发布于 2022-08-01 15:24:30
我找到了一个解决方案,比如您有一个用户模式,它可以跟踪用户的朋友。
const userSchema = new Schema({
name: String,
friends: [{ type: ObjectId, ref: 'User' }]
});填充可以让您获得用户朋友的列表,但是如果您也想要用户的朋友朋友呢?指定填充选项,告诉猫鼬填充所有用户朋友的朋友数组:
User.
findOne({ name: 'Val' }).
populate({
path: 'friends',
// Get friends of friends - populate the 'friends' array for every friend
populate: { path: 'friends' }
});https://stackoverflow.com/questions/73195380
复制相似问题