首页 > 解决方案 > 为什么我不能在不同的 mongodb 集合中有类似的索引(索引)?

问题描述

我正在为我正在从事的一些项目使用 Nest js,我决定使用 Typeorm。我知道直接使用 Mongoose 可能会更好,但我希望将来可以选择轻松迁移到 Postgress 或 MySQL。

根据我对 MongoDB 的理解,我只能在每个集合中拥有一个唯一的索引,但我可以在不同的集合、同一个数据库中拥有同名的索引。如果我错了,请纠正我。

我有两个实体FooBar

@Entity()
export class Foo {
  // Other columns

  @Column()
  @Index({ unique: true })
  email: string;

  // Other columns
}

@Entity()
export class Bar {
  // Other columns

  @Column()
  @Index({ unique: true })
  email: string;

  // Other columns
}

在我的 typeorm 配置中,我已经启用,synchronization因为我处于开发模式。我希望创建两个实体/集合而不会失败,但是当我启动我的应用程序时出现错误

MongoError: Index with name: [INDEX_NAME] already exists with a different name

在使用 MongoDB Compass 检查数据库时,我发现提到的 [INDEX_NAME] 是Foo实体的电子邮件索引。

有什么我想念的东西,因为我无法完全理解它吗?

标签: mongodbnestjstypeormmongodb-indexes

解决方案


推荐阅读