Announcement Announcement Module
No announcement yet.
Spring data MongoDb: MappingMongoConverter remove _class Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring data MongoDb: MappingMongoConverter remove _class

    The default MappingMongoConverter adds a custom type key ("_class") to each object in the database. So, if I create a Person:

        package my.dto;
        public class Person {
            String name;
            public Person(String name) {
       = name; 
    and save it to the db:

        MongoOperations ops = new MongoTemplate(new Mongo(), "users");
        ops.insert(new Person("Joe"));
    the resulting object in the mongo will be:

        { "_id" : ObjectId("4e2ca049744e664eba9d1e11"), "_class" : "my.dto.Person", "name" : "Joe" }


    1. What are the implications of moving the Person class into a different namespace?

    2. Is it possible not to pollute the object with the "_class" key; without writing a unique converter just for the Person class?

    Also posted on StackOverflow.
    Last edited by georgecalm; Jul 24th, 2011, 07:18 PM. Reason: Adding link to SO question.

  • #2
    Did you ever find a workaround for this?

    So what is the story with this? Is there no way to prevent the "_class" field from getting stored in MongoDB?


    • #3
      You can generally prevent the _class attribute being written by reconfiguring the DefaultMongoTypeMapper instance on MappingMongoConverter and setting the key to null:

      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:

      Map<? extends Class<?>, String> typeMap = … // Build map of types to type aliases
      MongoTypeMapper mapper = new DefaultMongoTypeMapper("_class", new ConfigurableTypeInformationMapper(typeMap));
      MappingMongoConverter converter = new MappingMongoConverter();
      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.


      • #4
        Thanks so much for your reply.

        I will give your suggestions a try!


        • #5
          Sorry for waking up this old thread.
          MappingMongoConverter doesn't seem to have a no-arg constructor; am I missing something?

          If there's a new answer to the question (how to remove _class), can someone please reply?


          • #6
            Created it as in the MongoTemplate source:

            MappingMongoConverter converter = new MappingMongoConverter(factory, new MongoMappingContext());\

            it works.