Hello,
I have an issue when query data from a partitioned collection.
I create a CosmosDB with Mongo API on Azure and use Mongo Driver for .Net version 2.5.1 to query data.
I have a collection "data" that is partitioned with partition key "dataType".
In the .Net project, I create an Data model to present a document in "data" collection as below:
class Data
{
[BsonElement("DataType")]
public string DataType {get; set;}
public long DateTime {get; set; }
}
I queried data form "data" collection with the statement below:
var filter = new BsonDocument("dataType", "Temperature");
SensorDatas.Find(filter).SortByDescending(s => s.DateTime).ToListAsync();
This statement works well when "data" collection is a single partition collection.
But with partitioned collection, I got an exception with message:
"Cross partition query with TOP/ORDER BY or aggregate functions is not supported"
This issue only happens when the number of document in result is bigger batch size (Default batch size = 101).
I changed the query statement to get number of document instead of list document, it works:
SensorDatas.Find(filter).SortByDescending(s => s.DateTime).Count();
So I think, the exception happens when mongo driver downloads list document in CosmosDB by many batches.
I also test with Robo3t with the query below, it worked:
db.getCollection('data').find({dataType: 'Temperature'}).sort({dateTime: -1})
I applied a workaround that setting batch size in FindOptions to a big number (One billion), to be sure all documents a return in a batch.
But I think, it's not a good solution.
Did I do any wrong thing?
Could some one give me some advice to resolve this issue?
Thanks and best regards,
Cuong Nguyen