【问题标题】:Add Id of created document in another document in mongoose在猫鼬的另一个文档中添加创建文档的 ID
【发布时间】:2021-06-06 07:19:32
【问题描述】:

我想在创建后将文档的 ID 保存到同一集合中的另一个文档中。 有没有办法做到这一点,只需在 mongoose 中进行一次查询?

【问题讨论】:

  • 不可能只用一个查询

标签: typescript mongodb mongoose nest


【解决方案1】:

您可以在模型文件中使用 post 中间件:

    schema.post('save', function(doc) {
     otherDocument.save()
});

然后创建一个文档但不保存它,所以它不会有 Id_ 。 然后保存第一个文档,这将触发 de post 函数中的 save()。

有关更多信息,请查看 mongoose 文档mongoose post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2022-12-23
    • 2016-04-20
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多