node.js - 当 id 引用其他模型的子文档时如何填充 id
问题描述
我正在尝试填充引用不同模型的子文档的对象 id 数组,我还没有创建该子文档的模型。
这是示例
const UserSchema = new Schema({
name: {
type: String,
},
interests: [{ type: Schema.Types.ObjectId, ref: 'Interest' }],
}, {timestamps: true});
const Users = model('User', UserSchema);
const SubCategorySchema = new Schema({
name: { type: String }
});
export const InterestSchema = new Schema({
categoryName: { type: String },
subCategory: [SubCategorySchema]
});
const Interests = model('Interest', InterestSchema);
兴趣文件
[
{
"_id": "60bffb7439ffa329c0904634",
"categoryName": "",
"subCategory": [
{
"_id": "60bffbd83d7f4e55a086c966",
"name": "Hedge Fund"
},
{
"_id": "60bffc163d7f4e55a086c967",
"name": "Private Equity"
},
{
"_id": "60bffc773d7f4e55a086c969",
"name": "Venture Capital"
}
]
}
]
用户文件:
[{
name: "test",
interests: ["60bffc163d7f4e55a086c967"]
}]
所以现在interests
数组我有子类别文档的对象ID,现在这就是我尝试过的
Users.find({}).populate({path: "Interest.subCategory"})
但它没有工作
解决方案
推荐阅读
- javascript - 如何使用反应获取文本输入字段的值
- swift - 仍然无法删除 tableviewcells
- python - 无法使用 pyside Qprocess 启动 exe 文件
- c - 使用 clang 从 C 代码生成 SIMD 代码
- python - 如何评估 XGboost 给出的预测概率?
- angular - 上传到 S3 存储桶的图像在浏览器上不可见
- dart - 当内容类型设置为 text/html 时,Dart http 服务器冻结
- python - 有没有办法从输入中删除多余的空格/格式?
- swift - UIScrollViewDelegate 方法仅在视图初始化时调用
- python - 具有多列的命名聚合