I want to write a spring-boot program to get values of name, id, and key where abc.active is true. I have written some code
@Repository
public interface SwitchRepoDao extends MongoRepository< SwitchRepo, String> {
public List<SwitchRepo> findByAbc_active(boolean active);
}
also, I have written class for interface.
@Document(collection="switchrepo")
public class SwitchRepo{
@Id
private String id;
private String type;
private List<Abc> abc;
// with getters and setters also constructors.
And Abc is class.
public class Abc{
private String name;
private String id;
private String key;
private boolean active;
This is the code I am using to display output.
@Bean
CommandLineRunner runner(SwitchRepoDao switchRepoDao) {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
Iterable<SwitchRepo> personList = switchRepoDao.findAllWithStatus(true);
System.out.println("Configuration : ");
for (SwitchRepo config : personList)
{
System.out.println(config.getRegistries().toString());
}
}
};
}
Can anyone please help me with this. For any query related question do comment. Thank You in advance.
Given below is MongoDB Collection from database test. and collection name is switchrepo.
"_id" : "1234567890",
"type" : "xyz",
"abc" : [
{
"name" : "test",
"id" : "test1",
"key" : "secret",
"active" : true
},
{
"name" : "test2",
"id" : "test12",
"key" : "secret2",
"active" : false
}
]
}
In response, I need output as
"id" : "test1",
"key" : "secret",
"active" : true
because active is true in that sub-document array.
Actual Result what I got is "abc" : [{"name" : "test","id" : "test1","key" : "secret","active" : true},{"name" : "test2","id" : "test12","key" : "secret2","active" : false}]