title: How can I distinct with 2 key value in mongodb with Java maven?
body: <pre><code>import com.mongodb.client.AggregateIterable;
  import com.mongodb.client.DistinctIterable;
  import com.mongodb.client.MongoClient;
  import com.mongodb.client.MongoClients;
  import com.mongodb.client.MongoCollection;
  import com.mongodb.client.MongoCursor;
  import com.mongodb.client.MongoDatabase;
  import static com.mongodb.client.model.Filters.eq;
  import com.mongodb.client.model.Projections;
  import com.mongodb.client.model.Sorts;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import org.apache.logging.log4j.spi.LoggerContext;
  import org.bson.Document;
  import org.bson.conversions.Bson;
  import org.slf4j.LoggerFactory;
  
  /*
   * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
   * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
   */
  
  /**
   *
   * @author matth
   */
  public class finder {
      ArrayList&lt;String&gt; brands,models;
      String uri = &quot;mongodb+srv://admin:adr@atlascluster.xcy35.mongodb.net/myFirstDatabase?retryWrites=true&amp;w=majority&quot;;
      public String CarBrandName=&quot; &quot;;
      public finder(String willsearch){
          
  
          //if equals to brand
  
          try (MongoClient mongoClient = MongoClients.create(uri)) {
              MongoDatabase database = mongoClient.getDatabase(&quot;AutomobileSubMaterialsSystem&quot;);
              MongoCollection&lt;Document&gt; collection = database.getCollection(&quot;AutomobileParts&quot;);
              
              if(&quot;Brand&quot;.equals(willsearch)){
              brands=new ArrayList&lt;String&gt;();
    
              MongoCursor&lt;String&gt; c=collection.distinct(willsearch,String.class).iterator();
              
              while(c.hasNext()){
                  brands.add(c.next());
              }
              for (int i = 0; i &lt; brands.size(); i++) {
                  System.out.println(brands.get(i));
                  System.out.println(brands.size());
              }}
              
              //if equals to brand
              else if(&quot;Model&quot;.equals(willsearch)){
                  models=new ArrayList&lt;String&gt;();
                  AggregateIterable&lt;Document&gt; output=collection.aggregate(Arrays.asList(new Document(&quot;$match&quot;,CarBrandName)));
                  for(Document dobject : output){
                      
                      System.out.println(dobject.get(&quot;Model&quot;));
                  }
                  
              } 
          
          }
              
          }
             
  }
       
  </code></pre>
  <p>I have database/collection/document.Documnent example:</p>
  <p>Brand
  :
  &quot;TOYOTA&quot;
  Model
  :
  &quot;COROLLA&quot;
  Year
  :
  2022
  EngineNo
  :
  0
  PieceName
  :
  &quot;roof&quot;
  NumberOfPiece
  :
  23</p>
  <p>I get all distinct brand but i cannot get to Brand's Model.My last 4 hour passed with this thing.How can i make multiple filter in java any way to do it?Thanks</p>