question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

deleteById() / deleteAll() not working with indexed properties

See original GitHub issue

I have found another bug which prevents me from switching from fongo to mongo-java-server.

If you modify an indexed property of a document and then delete the document, no new document can be inserted with the value of the originally indexed property.

Here is a test case to reproduce the problem:

@RunWith(SpringRunner.class)
@SpringBootTest(classes={Issue.TestConfiguration.class})
public class Issue {

	@Autowired private TestRepository repository;

	@Test
	public void testDeleteWithUniqueIndexes() throws Exception {
		TestEntity document = repository.save(new TestEntity("DOC_1", "Text1"));

		// update value of indexed property
		document.setText("Text1 (updated)");
		repository.save(document);

		// delete document (deleteAll() does not work either)
		repository.deleteById("DOC_1");

		// duplicate key error
		repository.save(new TestEntity("DOC_1", "Text1"));
	}


	@Configuration
	@EnableMongoRepositories(basePackageClasses={TestRepository.class})
	protected static class TestConfiguration {
		@Bean
		public MongoTemplate mongoTemplate(MongoClient mongoClient) {
			return new MongoTemplate(mongoDbFactory(mongoClient));
		}

		@Bean
		public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
			return new SimpleMongoDbFactory(mongoClient, "test");
		}

		@Bean(destroyMethod="shutdown")
		public MongoServer mongoServer() {
			MongoServer mongoServer = new MongoServer(new MemoryBackend());
			mongoServer.bind();
			return mongoServer;
		}

		@Bean(destroyMethod="close")
		public MongoClient mongoClient(MongoServer mongoServer) {
			return new MongoClient(new ServerAddress(mongoServer.getLocalAddress()));
		}
	}
}
@Document(collection="test")
public class TestEntity {
	@Id private String id;
	@Indexed(unique=true) private String text;
	public TestEntity() {
	}
	public TestEntity(String id, String text) {
		this.id = id;
		this.text = text;
	}
	public String getId() {
		return id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}
public interface TestRepository extends MongoRepository<TestEntity, String> {
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bwaldvogelcommented, Dec 28, 2018

It’s caused by missing support for sparse indices. I’m currently working on it and hope to get it pushed soon.

0reactions
bwaldvogelcommented, Jan 1, 2019

That’s great to hear 😀

I’ve just released version 1.11.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

JPA Query to deleteById for a Composite Key declared using ...
MyIdClass. Use setObject() with an explicit Types value to specify the type to use. after seeing the actual query generated for a batch...
Read more >
CrudRepository (Spring Data Core 3.0.0 API)
Deletes a given entity. void. deleteAll(). Deletes all entities managed by the repository. void.
Read more >
Spring Data JPA CrudRepository delete() and deleteAll()
In Spring Data JPA Repository is top-level interface in hierarchy. Here we are going to see delete() and deleteAll() method of CrudRepository.
Read more >
org.springframework.data.jpa.repository.support ... - Tabnine
notNull(entities, "The given Iterable of entities not be null! ... @Transactional @Override public void deleteAll() { for (T element : findAll()) ...
Read more >
ReactorCrudRepository (javadoc 2.4.0 API) - GitHub Pages
Deletes a given entity. reactor.core.publisher.Mono<java.lang.Long>, deleteAll(). Deletes all entities managed by the repository.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found