You can generally prevent the _class attribute being written by reconfiguring the DefaultMongoTypeMapper instance on MappingMongoConverter and setting the key to null:
Code:
MappingMongoConverter converter = new MappingMongoConverter();
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
The type information inside the document actually serves as type hint in case you're querying the the collection for superclasses or interfaces. If the type information in the document is not more concrete than the one actually requested is not considered. To be more safe agains potential refactorings consider using ConfigurableTypeInformationMapper with DefaultMongoTypeMapper like:
Code:
Map<? extends Class<?>, String> typeMap = … // Build map of types to type aliases
MongoTypeMapper mapper = new DefaultMongoTypeMapper("_class", new ConfigurableTypeInformationMapper(typeMap));
MappingMongoConverter converter = new MappingMongoConverter();
converter.setTypeMapper(mapper);
Note that by default we will setup a MongoTypeMapper inspecting the MappingContext for type aliases that can be annotated with @TypeAlias to the entity classes. So if you do so, we will write the alias instead of the class name which allows moving the class pretty much everywhere.