javascript - Mongoose 在创建时自动填充
问题描述
我正在使用 mongoose 插件autopopulate
从单独的集合中填充文档。这是一个简化的架构:
const PostSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true,
autopopulate: {
select: ['firstName', 'lastName', 'email', 'username']
}
},
comment: { type: String }
}
)
现在,当我创建一个帖子时
const post = await Post.create({ user, comment });
即使它显示在数据库和后续查询中,它也会为填充的用户返回 null。我已经使用了该{ new: true }
选项,findByIdAndUpdate
但它似乎不适用于create
. 关于如何创建以返回填充的文档的任何想法?
解决方案
推荐阅读
- sapui5 - SAPUI5 应用程序中的 JQuery 日期滑块
- jquery - 隐藏除当前弹出窗口之外的所有弹出窗口?
- php - 从程序将数据保存或存储在数据库中
- flutter - Flutter ListWheelScrollView
- android - 从与 FileProvider 共享大 pdf 文件的电子邮件客户端返回后,onCreate 被调用
- html - div 被放在上面的 div 中
- jsp - 为什么会话在我的 JSP 页面中返回 null?
- powershell - CSV 为空以导出广告用户的自定义属性
- runtime-error - 打开 SAP 时出现“nwbc.exe 已停止工作”错误
- c# - 在图片框中查找和显示图像