如何在使用Spring时更新MongoDb中的索引?

huangapple go评论123阅读模式
英文:

How to update index in MongoDb when using Spring?

问题

以下是翻译好的部分:

"我相信上面的代码意味着firstNamelastName的组合应该始终是唯一的。

现在,如果我想在这些约束中包括另一个列,比如我现在想要:

  1. @Data
  2. @CompoundIndex(
  3. name = "unique_name_with_age",
  4. def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
  5. unique = true)
  6. @Document
  7. public class Person {

我希望ssn字段也被考虑在唯一约束中。我的问题是,我该如何进行此更新?

在传统的关系型数据库中,比如PostgreSQL,我需要在数据库上运行一个命令来创建这个唯一约束,如果需要的话,运行迁移脚本来确保已存在的数据不违反这个新约束。

但是我不熟悉MongoDB,所以不确定在使用Spring Data MongoDB注解时如何进行这些更新。"

英文:

Let say I have class annotated with the following

  1. @Data
  2. @CompoundIndex(
  3. name = "unique_name_with_age",
  4. def = "{ 'firstName':1 , 'lastName':1 }",
  5. unique = true)
  6. @Document
  7. public class Person {

I believe the above means that the combination of firstName and lastName should always be unique.

Now what happens if I want to include another column in this constraints. Let say I now want

  1. @Data
  2. @CompoundIndex(
  3. name = "unique_name_with_age",
  4. def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
  5. unique = true)
  6. @Document
  7. public class Person {

so I want the ssn field to be considered in the unique constraints. My question is, how do I go about making this update?

In a typical RDBMS like postgres, I will need to run a command on the database that creates this unique constraints, and if need be, run a migration scripts to ensure already existing data does not violates this new constraints.

But I am not familair with MongoDB so not sure how to go about making these kinds of updates when using Spring data Mongodb annotations.

答案1

得分: 1

Here are the translated code sections:

First Solution using Shell

  1. # 删除名为 'unique_name_with_age' 的索引
  2. db.person.dropIndex('unique_name_with_age')
  3. # 创建新的索引
  4. db.person.createIndex(
  5. {
  6. 'firstName': 1, 'lastName': 1, 'ssn': 1
  7. },
  8. {
  9. "name": "unique_name_with_age",
  10. "unique": true
  11. }
  12. )

Second Solution using Mongock

这个迁移解决方案使用了 Mongock。在您的情况下,索引具有相同的名称,这增加了复杂性。下面我解释了步骤,但为了更清晰,我还创建了这个 GitHub 仓库,您可以尝试使用它来查看 Mongock 的操作。迁移将需要两个步骤:

  1. 删除现有的索引

这可以通过将 Person 类上现有的 CompoundIndex 注释掉,并创建一个 Mongock 变更集以删除它来实现。

  1. @ChangeUnit(id = "person-updater-changeset", order = "1")
  2. public class PersonUpdaterChange {
  3. ...
  4. @Execution
  5. public void changeSet() {
  6. // 删除原始索引
  7. mongoTemplate.indexOps(Person.class).dropIndex("unique_name_with_age");
  8. }
  9. @RollbackExecution
  10. public void rollback() {
  11. // 在这里提供回滚操作
  12. }
  13. }

然后运行应用程序。

  1. 更新 person 实体上的索引
  1. @CompoundIndex(
  2. name = "unique_name_with_age",
  3. def = "{ 'firstName': 1, 'lastName': 1, 'ssn': 1 }",
  4. unique = true)

然后运行应用程序。

英文:

First Solution using Shell

  1. db.person.dropIndex('unique_name_with_age')
  2. db.person.createIndex(
  3. {
  4. 'firstName':1 , 'lastName':1, 'ssn':1
  5. },
  6. {
  7. "name": "unique_name_with_age",
  8. "unique": true
  9. }
  10. )

Second Solution using Mongock

This solution for migration uses Mongock. A complexity in your case is that the index has the same name. Below I have explained the steps, but for clarity I have also created this GitHub repository which you can try out to see how Mongock operates. The migration will require two steps:

  1. Drop the existing index

This can be achieved by commenting out the existing CompoundIndex on your Person class and by creating a Mongock changeset to drop it.

  1. @ChangeUnit(id = "person-updater-changeset", order = "1")
  2. public class PersonUpdaterChange {
  3. ...
  4. @Execution
  5. public void changeSet() {
  6. //Drop the original index
  7. mongoTemplate.indexOps(Person.class).dropIndex("unique_name_with_age");
  8. }
  9. @RollbackExecution
  10. public void rollback() {
  11. // Provide a rollback here
  12. }
  13. }

And run the application.

  1. Updating the index on the person Entity
  1. @CompoundIndex(
  2. name = "unique_name_with_age",
  3. def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
  4. unique = true)

And run the application.

huangapple
  • 本文由 发表于 2023年8月11日 01:47:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定