首页 > 解决方案 > Mongoose 更新将虚拟填充保存到文档

问题描述

由于某种原因,猫鼬没有正确更新。它正在添加到我Document的虚拟中。

const BlogSchema: Schema = new Schema<Blog>({
  clientId: {
    type: Schema.Types.ObjectId
  }
  //...
})

ClientSchema.virtual('client', {
  ref: 'clients',
  localField: 'clientId',
  foreignField: '_id',
  justOne: true,
});

//...

return this.clientScreeningsModel
      .findById(id)
      .populate('client', 'email username')
      .lean(true);

这工作正常。

但是在客户端,我说我想更新或创建一个新的Document,然后我发送整个Object

例如:

{
  _id: 'something',
  clientId: 'previousId',
  client: {
    email: 'foo@bar.com',
    username: 'JohnDoe'
  }
}

它也会将客户端对象保存到我的Document,猫鼬不知道它是虚拟的吗?

在此先感谢您的帮助:)

标签: node.jstypescriptmongoosenestjs

解决方案


推荐阅读