Fetching collection independently of database, from a custom method is not possible

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • No version
    • Affects Version/s: None
    • Component/s: None
    • None
    • Environment:
      OS:
      node.js / npm versions:
      Additional info:
    • Not Needed
    • Developer Tools

      Problem Statement/Rationale

      public class JavaDriverRepository {
          private MongoDatabase database;
      
          public static MongoCollection<Document> getCustomCollection(MongoDatabase database) {
              return database.getCollection("movies");
          }
      
          public static MongoCollection<Document> getNamespace(MongoClient client) {
              return client.getDatabase("sample_mflix").getCollection("movies");
          }
      
          public MongoDatabase getDatabase(MongoClient client) {
              return client.getDatabase("sample_mflix");
          }
      
          public void test(String[] args) {
              String uri = "mongodb://localhost:27017";
              try (MongoClient client = MongoClients.create(uri)) {
                  database = getDatabase(client);
                  // This works because database reference is known here
                  // MongoCollection<Document> collection = database.getCollection("movies");
                  
                  // This also works because database reference is within the collection chain
                  // MongoCollection<Document> collection = getNamespace(client)
                  
                  // This does not work because database is left out of the parseable chain
                  // but it should
                  // MongoCollection<Document> collection = getCustomCollection(database);
                  collection.find(Filters.eq("something", "vakye"));
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
       

      Please be sure to attach relevant logs with any sensitive data redacted.
      How to retrieve logs for: Compass; Shell

      Steps to Reproduce

      How could an engineer replicate the issue you’re reporting?

      Expected Results

      What do you expect to happen?

      Actual Results

      What do you observe is happening?

      Additional Notes

      Any additional information that may be useful to include.

              Assignee:
              Himanshu Singh
              Reporter:
              Himanshu Singh
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: