Search code examples
mongodbindexingunique

Create unique index error on CosmosDB (Mongo API)


I am trying to create an unique index on some of the collections on our CosmosDB. Per MS document https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb-indexing : Unique indexes can be created only when the collection is empty (contains no documents).

So I cleanup all the data on the collection, however I was encountering the error, the message was :

Error=13, Details='Response status code does not indicate success: Forbidden (403); Substatus: 0; ActivityId: xxx, Reason: (Message: {"serializedCollection":"{"Errors":["The unique index cannot be modified. To change the unique index, remove the collection and re-create a new one."]}","serializedOffer":"","serializedPartitionKeyRanges":[],"serializedPartitions":[],"collectionRemoteStorageSecurityIdentifier":"xxx","collectionChildResourceNameLimitInBytes":-1,"collectionChildResourceContentLengthLimitInKB":-1,"uniqueIndexNameEncodingMode":0,"uniqueIndexReIndexingState":0} ActivityId: xxx, Request URI: /apps/xxx/xxx/partitions/xxx/replicas/xx, RequestStats: RequestStartTime: 2020-12-02T23:25:04.2104773Z, RequestEndTime: 2020-12-02T23:25:04.2204125Z, Number of regions attempted:1 ResponseTime: 2020-12-02T23:25:04.2204125Z, StoreResult: StorePhysicalAddress: rntbd://10.0.0.29:11000/apps/xxx/services/xx/partitions/xx/replicas/xx, LSN: 41, GlobalCommittedLsn: 41, PartitionKeyRangeId: , IsValid: True, StatusCode: 403, SubStatusCode: 0, RequestCharge: 1.57, ItemLSN: -1, SessionToken: -1#41, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: Replace , SDK: Microsoft.Azure.Documents.Common/2.11.0, Please see CosmosDiagnostics, Windows/10.0.17763 cosmos-netstandard-sdk/3.3.2);

I then tried to add only the field name , then it was throwing errors like:

Cannot create unique index when collection contains documents

We are using CosomosDB which supports Mongodb 3.6. I was using the command: db.CollectName.createIndex({"fieldname" : 1}, {"unique" : true } )

Any ideas why I am getting the errors?

Thanks,


Solution

  • Not sure what happened but it suddenly starts to work now. So the step is to clean out the data in the collection. Run the script to add the unique index and then load the data back. I am using Robo 3t to connect to the CosmosDB and run all the commands through Robo3T command line.