I and storing Doc objects in the mongodb repository ensuring that the barcode property is unique across the collection:
@Document(collection = "Doc")
public class Doc {
@Id
private String id;
@Indexed(unique = true)
private String barcode;
...
}
I also store a DocAudit collection which stores lists of Doc as sub-documents:
@Document(collection = "DocAudit")
public class DocAudit {
@Id
private String id;
private List<Doc> docs;
...
}
As DocAudit entries are generated, the same Doc can be present in the docs list of two or more DocAudit entries.
The @Indexed(unique = true) on the barcode property of the Doc object is automatically generating mongodb indexes on Doc.barcode (desired) and DocAudit.docs.doc.barcode (undesired)
What's the best way to avoid the automatic generation of the undesired index? Alternatively, what's the best way to remove the undesired index as soon as it is created?