How do I disable mapping of fields for Spring Data MongoDB documents?

DanJ

I am using Spring Data to persist POJOs as documents in MongoDB via MongoRepository. It looks like Spring is automatically persisting both fields and getters to MongoDB.

In general I would like it to only persist getters and never automatically persist fields. I know about @Transient for one-off annotations, but would like to configure this as general behavior.

Is there a way to configure this?

Konrad Lötzsch

It can be done by writing your own custom Converters.

You state in your question that spring data mongodb persist both, fields and getters. To my knowlegde only fields are persisted. (See 11.1 in the docu: http://docs.spring.io/spring-data/mongodb/docs/1.6.3.RELEASE/reference/html/#mapping-conventions (1.6.3 is the version shipped by spring-boot 1.2.6, but it is the same in older versions and 1.8.0))

or an short example:

If you have an Pojo like this:

@Document
public class MyClass
{
    private ObjectId id;

    private String foo = "foo";

    public String getBar()
    {
        return "bar";
    }
}

and a Repository like this:

public interface MyClassRepository extends MongoRepository<MyClass,ObjectId>
{
}

and an application code like this:

public static void main(String[] args) throws UnknownHostException
{
    ApplicationContext ctx = SpringApplication.run(NewClass.class, args);
    MongoTemplate mongoTemplate = ctx.getBean(MongoTemplate.class);
    MyClass myClass = new MyClass();
    mongoTemplate.save(myClass);
    MyClassRepository myClassRepository = ctx.getBean(MyClassRepository.class);
    myClassRepository.save(myClass);
}

the following document is save (once by the template and then again by the repository:

{
    "_id" : ObjectId("560b97edcb60110890ab7119"),
    "_class" : "sandbox.MyClass",
    "foo" : "foo"
}

So, the getter was not used for conversion of the MyClass object.

The same documentation as cited above shows you how to write your own Converter and how to register it to the MongoTemplate (section 8.10). You could write some code here that uses the declared getters of your class and maps them onto fields of your document.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Data with mongodb - how can I access dynamic fields in a document?

From Dev

How can I define index on inherited fields in MongoDB with Spring Data?

From Dev

How to index on all fields in spring data mongodb?

From Dev

How do I disable Chrome from converting pdf documents into text

From Dev

How do I sanitize sensitive fields in MongoDB?

From Dev

How to disable spring-data-mongodb autoconfiguration in spring-boot

From Dev

Spring Data Solr Result Documents with unwanted fields

From Dev

how do I find all documents with a field that is NaN in MongoDB?

From Dev

In MongoDB, how do I search in an array of sub-documents?

From Dev

How do I get n set of documents in a sequential order Mongodb

From Dev

how do I find all documents with a field that is NaN in MongoDB?

From Dev

In MongoDB, how do I search in an array of sub-documents?

From Dev

MongoDB: How do I query for the number of elements in an array of documents/dictionaries?

From Dev

How to compare 2 fields in Spring Data MongoDB using query object

From Dev

How to save and query dynamic fields in Spring Data MongoDB?

From Dev

spring data mongodb enum mapping converter

From Dev

spring data mongodb _id mapping preference

From Dev

How can I disable recent documents in Unity?

From Dev

How do I rename fields when performing search/projection in MongoDB?

From Dev

How do I access MongoDB data?

From Dev

mongodb find documents with fields with a data doesn't exist

From Dev

How do I disable Spring Dashboard on startup on GGTS?

From Dev

How to update existing documents with Spring Data MongoDB type information after type renames?

From Dev

MongoDB Spring data comparison between fields

From Dev

Spring Data MongoDB Repositories Query multiple fields

From Dev

How do I disable a button using data bindings in Dart?

From Dev

How to remove only one or two fields from documents in mongodb?

From Dev

Mongodb : How to find documents in which fields match an ObjectId or a string?

From Dev

How to get all fields in a same level in embedded documents in MongoDB

Related Related

  1. 1

    Spring Data with mongodb - how can I access dynamic fields in a document?

  2. 2

    How can I define index on inherited fields in MongoDB with Spring Data?

  3. 3

    How to index on all fields in spring data mongodb?

  4. 4

    How do I disable Chrome from converting pdf documents into text

  5. 5

    How do I sanitize sensitive fields in MongoDB?

  6. 6

    How to disable spring-data-mongodb autoconfiguration in spring-boot

  7. 7

    Spring Data Solr Result Documents with unwanted fields

  8. 8

    how do I find all documents with a field that is NaN in MongoDB?

  9. 9

    In MongoDB, how do I search in an array of sub-documents?

  10. 10

    How do I get n set of documents in a sequential order Mongodb

  11. 11

    how do I find all documents with a field that is NaN in MongoDB?

  12. 12

    In MongoDB, how do I search in an array of sub-documents?

  13. 13

    MongoDB: How do I query for the number of elements in an array of documents/dictionaries?

  14. 14

    How to compare 2 fields in Spring Data MongoDB using query object

  15. 15

    How to save and query dynamic fields in Spring Data MongoDB?

  16. 16

    spring data mongodb enum mapping converter

  17. 17

    spring data mongodb _id mapping preference

  18. 18

    How can I disable recent documents in Unity?

  19. 19

    How do I rename fields when performing search/projection in MongoDB?

  20. 20

    How do I access MongoDB data?

  21. 21

    mongodb find documents with fields with a data doesn't exist

  22. 22

    How do I disable Spring Dashboard on startup on GGTS?

  23. 23

    How to update existing documents with Spring Data MongoDB type information after type renames?

  24. 24

    MongoDB Spring data comparison between fields

  25. 25

    Spring Data MongoDB Repositories Query multiple fields

  26. 26

    How do I disable a button using data bindings in Dart?

  27. 27

    How to remove only one or two fields from documents in mongodb?

  28. 28

    Mongodb : How to find documents in which fields match an ObjectId or a string?

  29. 29

    How to get all fields in a same level in embedded documents in MongoDB

HotTag

Archive