英文:
How to update index in MongoDb when using Spring?
问题
以下是翻译好的部分:
"我相信上面的代码意味着firstName
和lastName
的组合应该始终是唯一的。
现在,如果我想在这些约束中包括另一个列,比如我现在想要:
@Data
@CompoundIndex(
name = "unique_name_with_age",
def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
unique = true)
@Document
public class Person {
我希望ssn
字段也被考虑在唯一约束中。我的问题是,我该如何进行此更新?
在传统的关系型数据库中,比如PostgreSQL,我需要在数据库上运行一个命令来创建这个唯一约束,如果需要的话,运行迁移脚本来确保已存在的数据不违反这个新约束。
但是我不熟悉MongoDB,所以不确定在使用Spring Data MongoDB注解时如何进行这些更新。"
英文:
Let say I have class annotated with the following
@Data
@CompoundIndex(
name = "unique_name_with_age",
def = "{ 'firstName':1 , 'lastName':1 }",
unique = true)
@Document
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
@Data
@CompoundIndex(
name = "unique_name_with_age",
def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
unique = true)
@Document
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
# 删除名为 'unique_name_with_age' 的索引
db.person.dropIndex('unique_name_with_age')
# 创建新的索引
db.person.createIndex(
{
'firstName': 1, 'lastName': 1, 'ssn': 1
},
{
"name": "unique_name_with_age",
"unique": true
}
)
Second Solution using Mongock
这个迁移解决方案使用了 Mongock。在您的情况下,索引具有相同的名称,这增加了复杂性。下面我解释了步骤,但为了更清晰,我还创建了这个 GitHub 仓库,您可以尝试使用它来查看 Mongock 的操作。迁移将需要两个步骤:
- 删除现有的索引
这可以通过将 Person
类上现有的 CompoundIndex
注释掉,并创建一个 Mongock 变更集以删除它来实现。
@ChangeUnit(id = "person-updater-changeset", order = "1")
public class PersonUpdaterChange {
...
@Execution
public void changeSet() {
// 删除原始索引
mongoTemplate.indexOps(Person.class).dropIndex("unique_name_with_age");
}
@RollbackExecution
public void rollback() {
// 在这里提供回滚操作
}
}
然后运行应用程序。
- 更新 person 实体上的索引
@CompoundIndex(
name = "unique_name_with_age",
def = "{ 'firstName': 1, 'lastName': 1, 'ssn': 1 }",
unique = true)
然后运行应用程序。
英文:
First Solution using Shell
db.person.dropIndex('unique_name_with_age')
db.person.createIndex(
{
'firstName':1 , 'lastName':1, 'ssn':1
},
{
"name": "unique_name_with_age",
"unique": true
}
)
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:
- 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.
@ChangeUnit(id = "person-updater-changeset", order = "1")
public class PersonUpdaterChange {
...
@Execution
public void changeSet() {
//Drop the original index
mongoTemplate.indexOps(Person.class).dropIndex("unique_name_with_age");
}
@RollbackExecution
public void rollback() {
// Provide a rollback here
}
}
And run the application.
- Updating the index on the person Entity
@CompoundIndex(
name = "unique_name_with_age",
def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
unique = true)
And run the application.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论