Spring 如何删除Mongo中嵌套对象的唯一索引
我使用的是弹簧靴2.0.0.M3。我有以下对象结构:Spring 如何删除Mongo中嵌套对象的唯一索引,spring,mongodb,spring-boot,Spring,Mongodb,Spring Boot,我使用的是弹簧靴2.0.0.M3。我有以下对象结构: @Document(collections="note") public class Note { String id; @Indexed(background=true,unique=true) String requestid; } @Document(collection="noteExpression") public class NoteExpression { public static class Err
@Document(collections="note")
public class Note {
String id;
@Indexed(background=true,unique=true)
String requestid;
}
@Document(collection="noteExpression")
public class NoteExpression {
public static class Error {
private DateTime dateTime = DateTime.now();
private Note note;
private String exception;
}
String id;
//Some other fields
Error error;
}
在NoteExpression的对象中,我需要存储发生意外事件时的错误信息。一切似乎都很好。但问题是Mongo将为NoteExpression.Error中的嵌套属性注释创建唯一索引
请参见下面的Mongo命令的结果:
>db.noteExpression.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.noteExpression"
},
{
"v" : 2,
"unique" : true,
"key" : {
"errors.note.requestid" : 1
},
"name" : "errors.note.requestid",
"ns" : "test.noteExpression",
"background" : true
}
]
在我的系统中,注释文档上requestid的唯一索引是必需的,但我不希望注释表达式文档上的唯一索引。有什么方法可以避免在NoteExpression上创建索引吗?MongoDB索引包括嵌套的(当然也包括引用的)文档。这通常是一个令人头痛的问题,我迄今为止找到的唯一解决方案是通过在save/insert操作中对其进行编码来实现唯一性约束,因此您必须首先查询数据库,检查前提条件,然后在满足前提条件的情况下保存/插入。我将进一步调查,看看Spring数据是否为此提供了一些解决方案