I have code like:
public Foo add(String firebaseId) {
Foo foo = fooRepository.findByFirebase(firebaseId);
if (foo == null) {
foo = new Foo();
}
foo.setFirebaseId(firebaseId);
return fooRepository.save(foo);
}
public interface UsersPlayersRepository extends MongoRepository<Foo, ObjectId> {
Foo findByFirebaseId(String firebaseId);
}
@Document(collection = "foo")
public class Foo {
public Foo() {}
@Id
@JsonIgnore
private ObjectId id;
@JsonProperty
private String firebaseId;
....
but yet somehow in logs I later see errors on retrieval:
ERROR] 2019-12-27 01:15:07.985 [http-nio-9000-exec-408] [dispatcherServlet] - Servlet .service() for servlet [dispatcherServlet] in context with path [] threw exception [Re quest processing failed; nested exception is org.springframework.dao.IncorrectResultSi zeDataAccessException: Query { "$java" : Query: { "firebaseId" : "3OdjeUiCkOeKzH1ETvGL HjH0xLv1" }, Fields: { }, Sort: { } } returned non unique result.] with root cause
I want to make sure only unique firebase item is stored? Is there something wrong in my construction?