Reputation: 746
I have following entity-model which I'll save to my MongoDB:
@Document(collection = "googleplaygames")
@Component
public final class GooglePlayGame implements Serializable {
@Id
private String title;
private String genre;
private String price;
private LocalDate dateOfLastUpdate;
...
This code allows me to save duplicates Game objects. I found annotation @Indexed and rewrote code:
@Document(collection = "googleplaygames")
@Component
public final class GooglePlayGame implements Serializable {
@Indexed(unique=true)
private String title;
private String genre;
private String price;
private LocalDate dateOfLastUpdate;
...
Now if I'll try to save entity with same title, I'll receive org.springframework.dao.DuplicateKeyException
. Fair enough.
And I found this "error" in logs while Spring Boot app is starting:
2020-06-26 13:26:52,303 WARN [restartedMain] org.springframework.data.mongodb.core.index.JustOnceLogger: Automatic index creation will be disabled by default as of Spring Data MongoDB 3.x.
Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.
> -----------------------------------------------------------------------------------------
> @EventListener(ApplicationReadyEvent.class)
> public void initIndicesAfterStartup() {
>
> IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
>
> IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
> resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
> }
> -----------------------------------------------------------------------------------------
But I want to update the enity, if any of other fields are different. For example, if I have an entity with title "Dead Cells" and version "1.0" in my db, I want to update this entity if version now is "1.1". But code above doesn't allow me to do this.
So what is this error? And how to update entity (doesnt allow duplicate by title field, but allow to rewrite entity, if other fields were changed).
Upvotes: 0
Views: 2196
Reputation: 1045
According to how you put the question, if you are updating a document, you shouldn't receive the DuplicateKeyException, this only occurs if you have another document with the index value duplicated. Check you are applying the save() method of your repository (as I presume you do) on and existent instance of a Mongo document.
If you want to control duplications, you could use a compound index. A compound index includes more than one field in its definition, so it's faster to make searches for including in the criteria the fields of the index. Including the unicity constraint you'll be able to forbid the duplication of such values in another documents.
Let's say, according to your example you want to avoid titles and genre duplication, then you could define an index as:
@Document(collection = "googleplaygames")
@CompoundIndex(def = "{'title':1,'genre':1}", unique = true)
@Component
public final class GooglePlayGame implements Serializable {
private String title;
private String genre;
private String price;
private LocalDate dateOfLastUpdate;
...
With this index, if you have a document:
{"title":"Pacman","genre":"arcade"}
And try to create a new document with the same values for title and genre you will get the DuplicateKeyException.
If you try to create a document:
{"title":"Pacman","genre":"mobile"}
The you will have two documents.
Upvotes: 1