0

First thanks in advance for the help.

I have an application with Spring Boot and mongo using Spring Data and I have something like this:

@Document("products")
class Product {
    @Id
    private String $id;
    private String $name;
    private Money price;
    private Money deliveryPrice;
}

class Money {
    private BigDecimal amount;
    private Currency currency;
}

If I save a product it saves like:

{
    _id: ObjectId("AAAA"),
    name: "Product 1",
    price: {
        amount: "100",
        currency: "EUR"
    },
    deliveryPrice: {
        amount: "10",
        currency: "EUR"
    }
}

It's correct but I don't like how it saves.

I would like to store like:

{
    _id: ObjectId("AAAA"),
    name: "Product 1",
    price: DecimalNumber(100),
    deliveryPrice: DecimalNumber(10),
    currency: "EUR"
}

That way I am not duplicating information that is not needed in all the prices value.

How can I solve this? Is this the best solution? In case not how should it be?

Thanks

Jorge
  • 73
  • 1
  • 9

1 Answers1

0
class Money {
    private BigDecimal amount;
    @Transient
    private Currency currency;
}

Now the currency property will not be persisted in MongoDB.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85