title: How to fix limit and orderBy in Mongodb using Java
body: <p>I am trying to fetch a list of documents using specified filters. I want to limit the output to 10 and orderBy a column of type long(totalTransactionCount that is found in the sample data posted below). However, the output I get is just 8 instead of 10, there are over 100 records in the collection that meets the requirement. gged the size of the arraylist, it returned 10. This implies that the issue is not coming from the arrayList rather from my loop.</p>
  <p>Here is the code</p>
  <pre><code> Bson filter = Filters.and(Filters.eq(&quot;type&quot;, &quot;CASH_OUT&quot;),
                  Filters.exists(&quot;successfulTransactionInfo.totalTransactionAmount&quot;),
                  AnalyticFilters.agentsFilter(queryParams));
        
  
          Bson orderBySort = orderBy(ascending(&quot;successfulTransactionInfo.totalTransactionCount&quot;));
  
          ArrayList&lt;TransactionAnalytic&gt; transactionAnalytics = transactionAnalyticCollection.find(filter).
                  sort(orderBySort).limit(10).into(new ArrayList&lt;&gt;());
  
   Set&lt;String&gt; agentIds = new HashSet&lt;&gt;();
  
      transactionAnalytics.forEach(transaction -&gt;{
          log.info(&quot;Here is the list of agentIds fetchec {}&quot;, agentIds);
          agentIds.add(transaction.getAgentID());
          try {
              BulkAgentInfoResponse agentPhoneNumber = getAgentPhoneNumber(agentIds, token);
              agentPhoneNumber.getData().forEach(tranx -&gt;{
                  if(tranx.getAgentID().equalsIgnoreCase(transaction.getAgentID())){
                      String name = tranx.getFirstName() + &quot; &quot; + tranx.getLastName();
                      result.put(transaction.getAgentID(), new AgentPerformanceDto(name,
                              transaction.getAgentID(), tranx.getPhoneNumber(), transaction.getSuccessfulTransactionInfo().getTotalTransactionAmount(),
                              transaction.getSuccessfulTransactionInfo().getTransactionCount()));
                  }
              });
  
          } catch (ExecutionException | IOException | InterruptedException e) {
              throw new RuntimeException(e);
          }
  
      });
      return result;
  </code></pre>
  <p>I am currently not passing anything into the query params therefore it is using the default parameter. I have tried to skip 2 (skip(2) but I still get 8 records instead of 10.</p>
  <p>This is a sample of the response data. I am ordering by totalTransactionCount. The one with totalTransaction is meant to be at the top and it should follow suit.</p>
  <pre><code>&quot;data&quot;: [
          {
              &quot;agentName&quot;: &quot;job o ajari&quot;,
              &quot;agentID&quot;: &quot;0001381&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 51466.0,
              &quot;cashOutCount&quot;: 7
          },
          {
              &quot;agentName&quot;: &quot;mabel ekporo&quot;,
              &quot;agentID&quot;: &quot;0001437&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 459899.25,
              &quot;cashOutCount&quot;: 7
          },
          {
              &quot;agentName&quot;: &quot;zion son&quot;,
              &quot;agentID&quot;: &quot;0000578&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 159317.5,
              &quot;cashOutCount&quot;: 6
          },
          {
              &quot;agentName&quot;: &quot;tukur saidu&quot;,
              &quot;agentID&quot;: &quot;0001304&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 5050.0,
              &quot;cashOutCount&quot;: 1
          },
          {
              &quot;agentName&quot;: &quot;obed eneh&quot;,
              &quot;agentID&quot;: &quot;0000885&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 44080.75,
              &quot;cashOutCount&quot;: 6
          },
          {
              &quot;agentName&quot;: &quot;bolanle olarotimi&quot;,
              &quot;agentID&quot;: &quot;0000995&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 41892.5,
              &quot;cashOutCount&quot;: 8
          },
          {
              &quot;agentName&quot;: &quot;Blessing Nana Mathew&quot;,
              &quot;agentID&quot;: &quot;0001487&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 10000.0,
              &quot;cashOutCount&quot;: 1
          },
          {
              &quot;agentName&quot;: &quot;anene chinonso&quot;,
              &quot;agentID&quot;: &quot;0001585&quot;,
              &quot;phoneNumber&quot;: &quot;01223445&quot;,
              &quot;cashOutAmount&quot;: 10300.0,
              &quot;cashOutCount&quot;: 1
          }
      ]
  </code></pre>
  <p>This is the input filter here. Like I stated earlier, I am not passing anything so, it's going to use the default value which is the current date minus 1. The collection contains data from August 1st - 22nd. Thus, there are over 100 records that meets the requirement.</p>
  <pre><code>public static Bson agentsFilter(MultivaluedMap&lt;String, String&gt; queryParams) throws ParseException{
          Bson filter = new Document();
          String startDate = queryParams.getFirst(&quot;startDate&quot;);
  
          String endDate = queryParams.getFirst(&quot;endDate&quot;);
  
          if (Objects.nonNull(startDate) &amp;&amp; Objects.nonNull(endDate)) {
              Date date1 = new SimpleDateFormat(DATE_FORMAT).parse(startDate);
              Date date2 = DateUtils.addHours(new SimpleDateFormat(DATE_FORMAT).parse(endDate), 24);
              filter = Filters.and(
                      filter,
                      Filters.and(
                              Filters.gte(CREATED_AT, date1),
                              Filters.lte(CREATED_AT, date2)
                      )
              );
          } else{
              Date defaultStartDate =  new SimpleDateFormat(DATE_FORMAT)
                      .parse(LocalDate.now().minusMonths(1).toString() );
              filter = Filters.and(filter, Filters.gte(CREATED_AT, defaultStartDate)
              );
          }
  
          return filter;
      }
  </code></pre>
  <p>The orderBy is meant to start with the largest value but it's just random. How do I fix this problem? It is expedient to state that I am using MongoJack and Dropwizard as the framework.</p>