Announcement Announcement Module
Collapse
No announcement yet.
Spring data MongoDb: MappingMongoConverter remove _class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

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

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

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

    Questions:

    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, 08: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?

    Comment


    • #3
      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.

      Comment


      • #4
        Thanks so much for your reply.

        I will give your suggestions a try!

        Comment


        • #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?

          Comment


          • #6
            Created it as in the MongoTemplate source:

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

            it works.

            Comment

            Working...
            X