Isuru Gunawardana
Isuru Gunawardana

Reputation: 2887

Number of records in the result causes -> Object reference not set to an instance of an object - Azure Cosmos Db

I'm getting the below error when retrieving data from cosmos db using java.

com.mongodb.MongoQueryException: Query failed with error code 1 and error message '[ActivityId=92f973a7-c33f-xxxx-xxxx-xxxxxxxxxxxxx] 
Error=1, Details='Response status code does not indicate success: InternalServerError (500); 
Substatus: 0; ActivityId: 00000000-0000-0000-0000-000000000000; 
Reason: (Object reference not set to an instance of an object.);' on server xxxx-xxxx-westus.mongo.cosmos.azure.com:10255

Below is the method used to get data.

public List<T> getData(String orKey,
                          String[] orValues,
                          LinkedHashMap<String, Object> andProperties,
                          String orderByKey, int offset, int limit) {

Bson[] orFilers = new Bson[orValues.length];
int counter = 0;
if (orValues.length > 0) {
  for (String s : orValues) {
    orFilers[counter++] = eq(orKey, s.trim());
  }
}

Bson[] andFilers = new Bson[andProperties.size()];
counter = 0;
if (andProperties.size() > 0) {
  for (Map.Entry<String, Object> entry : andProperties.entrySet()) {
    andFilers[counter++] = eq(entry.getKey(), entry.getValue());
  }
}

ArrayList<T> results = new ArrayList<>();
collection.find(and(or(orFilers), and(andFilers)))
        .skip(offset)
        .limit(limit)
        .sort(descending(orderByKey))
        .forEach((Consumer<T>) t -> {
          results.add(t);
        });
return results;

}

I did some debugging and found out when the results list has 102 or more records this error occurs. I found it by changing the limit and offset values.

when,

offset = 0 & limit = 101 => success
offset = 0 & limit = 102 => error
offset = 1 & limit = 102 => error
offset = 1 & limit = 105 => error
offset = 1 & limit = 101 => success

Note: There are 113 valid records for the given query.

Upvotes: 0

Views: 273

Answers (2)

Isuru Gunawardana
Isuru Gunawardana

Reputation: 2887

The issue was on the index field. I had created an index with the Date type field and some of the records had null value for that field.

public void createIndex(String keyString) {
    Document key = new Document(keyString, 1);
    this.collection.createIndex(key, new IndexOptions());
}

Changed that to a long type field and the issue got resolved. Why the earlier query gives error on 102 count is something that I couldn't find.

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222582

Have you set the MaxItemCount count value?

For pagination with Cosmosdb , the default value is 100.

You can specify the maximum number of items returned by a query by setting the MaxItemCount. The MaxItemCount is specified per request and tells the query engine will to return that number of items or fewer. You can set MaxItemCount to -1 if you don't want to place a limit on the number of results per query execution.

You can set the maxItemCount and try to execute again .

Upvotes: 1

Related Questions