Error creating events and sagas in axonframework db after migration of db from mongo db to azure mongodb(cosmosdb)

We have migrated recently from mongo db to azure mongodb which is cosmosDb.
We are using axon framework for storing events with the
axon version 4.1.1,
axon-mongo version 4.4 and
spring-boot version 2.3.0.RELEASE.
With the earlier mongodb, the collections domain events and saga was getting created automatically. The events and sagas is populated correctly without any issues.

But after the migration, few domain events are getting created but not all and also I can see ‘sagas’ collection is not creating automatically.

I created the sagas collection manually in the db but it didn’t work and I am getting the following errors
We are getting this exception when our application is booting up:

[agaProcessor]-0] o.a.e.TrackingEventProcessor: Fetch Segments for Processor ‘…SagaProcessor’ failed: Query failed with error code 2 and error message 'Error=2, Details='Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 09ec2c08-aed9-4cc9-8fdd-5f30f5e5121e; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 09ec2c08-aed9-4cc9-8fdd-5f30f5e5121e; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 09ec2c08-aed9-4cc9-8fdd-5f30f5e5121e; Reason: (Message: {​​​​​​​​​​​"Errors":[“The index path corresponding to the specified order-by item is excluded.”]}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Seems the issue is in the saga but not very clear.

Any help will be highly appreciated.

A quick Google on the exception description gave me this document:
https://docs.microsoft.com/en-us/answers/questions/258032/the-index-path-corresponding-to-the-specified-orde.html

From the response from Microsoft, this seems to be an CosmosDB specific issue. They require an index for all fields that are used in a sort. Apparently, other Mongo “distributions” don’t require that.

The sort that Axon does, is on the segment property of the Tracking Tokens collections (default name is trackingtokens). The solution would be to declare an index on that property.

For example:

mongoTemplate.trackingTokensCollection().createIndex(Indexes.ascending("segment"),
                                                             new IndexOptions().unique(false));

Hi @allardbz ,

Thanks for your reply.
With the solution that you have provided, now the ‘sagas’ collection has created automatically with index. But the error is still there. And also the remaining domain events and sagas events are still not getting created.

There might be something similar going on for the other queries. You can check the mongo extension to see which queries are being executed, and make sure that for each field used in a “where” clause, an index is created.
It seems that CosmosDB is not a 100% Mongo replacement. It’s a little bit stricter on the indices used.