首页 > 解决方案 > 如何使 mongoDB 子文档具有唯一 id 或递增 id?

问题描述

解决了

我有这个 MongoDB 文档:

{
  _id: "5e3bf72db5074c1e205409f5",
  todos: [
  {
    title: "title",
    description: "description",
    state: "state",
    priority: "priority"
  }
],
  title: "test project"
}

我想保留项目的主要 '_id:' 属性,但还要在我的 todos 中添加一个“_id:”或类似的东西,这样我就可以更轻松地单独对它们进行 CRUD 操作。有没有办法为他们制作一个唯一的 id 或自动递增的 id?最终结果将如下所示

有没有办法做到这一点?此外,任何其他建议表示赞赏。

{
  _id: another unique id or autoincrement key,
  title: "title",
  description: "description",
  state: "state",
  priority: "priority"
}

标签: jsondatabasemongodb

解决方案


推荐阅读