Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda