A
A
Antony2021-06-10 16:41:12
Java
Antony, 2021-06-10 16:41:12

How to substitute collection name from environment variables in spring data?

Actually there is a Mongolian base, it has some kind of collection.
There is an environment variable (or a string in application.properties - it doesn't matter in this case) "collections.collection1" which is equal to what.

There is this model:

@Data
@Document(collection = "${collections.collection1}")
public class MyModel {

    @Id
    private ObjectId _id;
    @Field
    private String myField
  //...

    @Override
    public boolean equals(Object o) {
        //...
    }

    @Override
    public int hashCode() {
        //...
    }
}


Actually, when I try to request something from the database, I get an error
Invalid collection name specified 'mymongodb.${collections.collection1}
those. the variable is not substituted here (for example, in RestController, such a substituted works fine).

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-06-10
Hasanly @azerphoenix

Good afternoon.
Here is a similar question - https://stackoverflow.com/questions/59900419/injec...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question