title: How to retrieve values of nested objects in a Mongo DB document using java
body: <p>I have a document like below:</p>
  <pre><code>&quot;application&quot; : &quot;test&quot;,
  &quot;QA1&quot; : {
      &quot;url&quot; : &quot;https://google.co.in&quot;,
      &quot;db&quot; : {
          &quot;userName&quot; : &quot;user&quot;,
          &quot;password&quot; : &quot;pswd&quot;
      }
  }
  </code></pre>
  <p>I want to retrieve the value &quot;https://google.co.in&quot; by calling the key &quot;url&quot;.</p>
  <p>While using the below shell command, I am able to retrieve the value as expected.
  <strong>db.getCollection('Application_Data').findOne({&quot;application&quot;:&quot;test&quot;}).QA1.url</strong></p>
  <p>But when I convert it to java code, it is throwing me null pointer exception:</p>
  <pre><code>cursor = collection.find(Filters.eq(&quot;application&quot;,&quot;test&quot;)).iterator();
          while (cursor.hasNext()) {
              value=cursor.next().get(&quot;QA1.url&quot;).toString();
              break;
          }
  </code></pre>
  <p>I also tried with projection like below to get the required values:</p>
  <pre><code>cursor = collection.find(Filters.and(Filters.eq(&quot;application&quot;,&quot;test&quot;))).projection(Projections.fields(Projections.include(&quot;QA1.url&quot;),Projections.excludeId())).iterator();
          while (cursor.hasNext()) {
              System.out.println(cursor.next().get(&quot;QA1&quot;).toString());
          }
  </code></pre>
  <p>This is giving me the output as below:</p>
  <blockquote>
  <p>Document{{url=https://google.co.in}}</p>
  </blockquote>
  <p>It is still not giving me the exact value. Appreciate your help on this.</p>