title: I want to write spring boot code to fetch data from mongoDB with condition
body: <p>I am able to fetch data from MongoDB using the following code but I want to expose that data from CommandLineRunner runner to other class.</p>
  
  <pre><code>@Repository
  public interface ToolsDao extends MongoRepository&lt; Tools, String&gt; {
       public List&lt;Tools&gt; findByRegistries_active(boolean active);
  }
  </code></pre>
  
  <p>Also, I have written class as with getters and setters with constructors.</p>
  
  <pre><code>public class Tools{
  
      @Id
      private String id;
      private String type;
      private List&lt;Registries&gt; registries;
  </code></pre>
  
  <p>And For registries I have,</p>
  
  <pre><code>public class Registries {
  
      private String name;
      private String url;
      private String secret;
      private boolean active;
  
  </code></pre>
  
  <pre><code>@Bean
  CommandLineRunner runner(ToolsDao toolsDao) {
      return new CommandLineRunner() {
          @Override
          public void run(String... args) throws Exception {
              Registries activeRegistry = null;
              Iterable&lt;Tools&gt; toolsList= toolsDao.findByRegistries_active(true);
              System.out.println("Configuration : ");
  
              for (Tools tools : toolsList) {
                  for (Registries registry : config.getRegistries()) {
                      if(registry.getActive()) {
                          activeRegistry = registry;
                          break;
                      }
                  }
              }
  
              System.out.println("active registry name is " + activeRegistry.getName());
              System.out.println("active registry url " + activeRegistry.getRegistryUrl());
  
          }
      };
  }
  </code></pre>
  
  <p>I am getting output in the above function but if I want to use in any other function then It is giving null values.</p>
  
  <pre><code>public class RegistryController2 {
  @Autowired
  ToolsDao toolsDao;
  public Registries getData() {
      Registries activeRegistry = null;
      Iterable&lt;Tools&gt; personList = toolsDao.findByRegistries_active(true);
      System.out.println("Configuration : ");
  
      for (Tools tools : toolsList) {
          for (Registries registry : tools.getRegistries()) {
              if(registry.getActive()) {
                  activeRegistry = registry;
                  break;
              }
          }
      }
  
      System.out.println("active registry name is " + activeRegistry.getName());
      System.out.println("active registry url " + activeRegistry.getRegistryUrl());
  }
  
  </code></pre>
  
  <p>MongoDB database :</p>
  
  <pre><code>Given below is MongoDB Collection from database test. and collection name is switchrepo.
  
  "_id" : "1234567890",
      "type" : "toolss",
      "registreis" : [ 
          {
              "name" : "test",
              "url" : "test1",
              "secret" : "secret",
              "active" : true
          }, 
          {
              "name" : "test2",
              "url" : "test12",
              "secret" : "secret2",
              "active" : false
          }
      ]
  }
  </code></pre>