Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Map one mongodb field to two POJO properties

I have a property on my mongodb entity, lets call it foo.

When I retrieve this property, i want its value to be set on two different properties of my POJO, foo and bar

So if a document is registered as:

{
  "_id": "xxxxxx",
  "foo": "123",
}

When the service retrieves it from the DB, i want the fetched entity to look like this:

{
  "_id": "xxxxxx",
  "foo": "123",
  "bar": "123",
}

Is there a way to achieve this only changing the entity class? I tried the following and it didn't work

@Getter
@Setter
@Document("test")
public class MyClass {
    @Id
    private String _id;

    @BsonProperty("foo")
    private String bar;

    private String foo;

}

I'm using Spring Webflux and Reactive MongoDB stack.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's not allowed to map multiple object fields to a single db field but you can redefine getter for the bar

@Getter
@Setter
@Document("test")
public class MyClass {
    @Id
    private String _id;

    @BsonProperty("foo")
    private String foo;

    private String bar;

    public String getBar() { 
       return foo; 
    }

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!