[BUG] Failed to create cosmos container - Partition key path /country is invalid for MongoDB API
Describe the bug
Attempt to create container with java-sdk 'com.azure:azure-cosmos:4.47.0', failed with error "Partition key path /country is invalid for MongoDB API"
String endPoint = "https://" + mongoDBAccount + ".documents.azure.com:443/";
CosmosClient client = new CosmosClientBuilder()
.endpoint(endPoint)
.key(mongoDBKey)
.buildClient();
CosmosDatabase cosmosDatabase = client.getDatabase(mongoDBDatabase);
cosmosDatabase.createContainerIfNotExists("test","/country");
My cosmos account:
- Azure Cosmos DB for MongoDB
- Server version: 4.2
Exception or Stack Trace ` {"ClassName":"CosmosException","userAgent":"azsdk-java-cosmos/4.47.0 MacOSX/13.4.1 JRE/11.0.18","statusCode":400,"resourceAddress":"https://return-cosmos-account-centralus.documents.azure.com:443/dbs/data-dev/colls","error":"{"code":"BadRequest","message":"Partition key path /test is invalid for MongoDB API.\r\nActivityId: 038fceb8-d821-4f38-abc5-fb7a09202bd7, Microsoft.Azure.Documents.Common/2.14.0, StatusCode: BadRequest","additionalErrorInfo":null}","innerErrorMessage":"Partition key path /test is invalid for MongoDB API.\r\nActivityId: 038fceb8-d821-4f38-abc5-fb7a09202bd7, Microsoft.Azure.Documents.Common/2.14.0, StatusCode: BadRequest","causeInfo":null,"responseHeaders":"{Transfer-Encoding=chunked, Strict-Transport-Security=max-age=31536000, Server=Microsoft-HTTPAPI/2.0, x-ms-gatewayversion=version=2.14.0, Date=Thu, 13 Jul 2023 10:36:06 GMT, x-ms-activity-id=038fceb8-d821-4f38-abc5-fb7a09202bd7, Content-Type=application/json}","requestHeaders":"[x-ms-cosmos-sdk-supportedcapabilities=1, Accept=application/json, x-ms-date=Thu, 13 Jul 2023 10:36:05 GMT, Content-Type=application/json]","cosmosDiagnostics":{"userAgent":"azsdk-java-cosmos/4.47.0 MacOSX/13.4.1 JRE/11.0.18","activityId":"038fceb8-d821-4f38-abc5-fb7a09202bd7","requestLatencyInMs":1505,"requestStartTimeUTC":"2023-07-13T10:36:05.809652Z","requestEndTimeUTC":"2023-07-13T10:36:07.315083Z","responseStatisticsList":[],"supplementalResponseStatisticsList":[],"addressResolutionStatistics":{},"regionsContacted":["central us"],"retryContext":{"statusAndSubStatusCodes":null,"retryLatency":0,"retryCount":0},"metadataDiagnosticsContext":{"metadataDiagnosticList":null},"serializationDiagnosticsContext":{"serializationDiagnosticsList":[{"serializationType":"CONTAINER_SERIALIZATION","startTimeUTC":"2023-07-13T10:36:05.811243Z","endTimeUTC":"2023-07-13T10:36:05.813846Z","durationInMilliSecs":2.603}]},"gatewayStatistics":{"sessionToken":null,"operationType":"Create","resourceType":"DocumentCollection","statusCode":400,"subStatusCode":0,"requestCharge":0.0,"requestTimeline":[{"eventName":"connectionCreated","startTimeUTC":"2023-07-13T10:36:05.817502Z","durationInMilliSecs":1119.77},{"eventName":"connectionConfigured","startTimeUTC":"2023-07-13T10:36:06.937272Z","durationInMilliSecs":0.084},{"eventName":"requestSent","startTimeUTC":"2023-07-13T10:36:06.937356Z","durationInMilliSecs":3.357},{"eventName":"transitTime","startTimeUTC":"2023-07-13T10:36:06.940713Z","durationInMilliSecs":372.643},{"eventName":"received","startTimeUTC":"2023-07-13T10:36:07.313356Z","durationInMilliSecs":0.823}],"partitionKeyRangeId":null,"exceptionMessage":"Partition key path /test is invalid for MongoDB API.\r\nActivityId: 038fceb8-d821-4f38-abc5-fb7a09202bd7, Microsoft.Azure.Documents.Common/2.14.0, StatusCode: BadRequest","exceptionResponseHeaders":"{Transfer-Encoding=chunked, Strict-Transport-Security=max-age=31536000, Server=Microsoft-HTTPAPI/2.0, x-ms-gatewayversion=version=2.14.0, Date=Thu, 13 Jul 2023 10:36:06 GMT, x-ms-activity-id=038fceb8-d821-4f38-abc5-fb7a09202bd7, Content-Type=application/json}","responsePayloadSizeInBytes":0},"samplingRateSnapshot":1.0,"systemInformation":{"usedMemory":"66897 KB","availableMemory":"4127407 KB","systemCpuLoad":"(2023-07-13T10:36:03.570043Z 40.7%), (2023-07-13T10:36:06.159384Z 40.4%)","availableProcessors":12},"clientCfgs":{"id":1,"machineId":"uuid:fc773e50-6572-44da-a2d6-dbdf478a7101","connectionMode":"DIRECT","numberOfClients":1,"clientEndpoints":{"https://return-cosmos-account.documents.azure.com:443/":1},"connCfg":{"rntbd":"(cto:PT5S, nrto:PT5S, icto:PT0S, ieto:PT1H, mcpe:130, mrpc:30, cer:true)","gw":"(cps:1000, nrto:PT1M, icto:PT1M, p:false)","other":"(ed: true, cs: false, rv: true)"},"consistencyCfg":"(consistency: null, mm: true, prgns: [])","proactiveInit":null}}} at com.azure.cosmos.BridgeInternal.createCosmosException(BridgeInternal.java:471) at com.azure.cosmos.implementation.RxGatewayStoreModel.validateOrThrow(RxGatewayStoreModel.java:448) at com.azure.cosmos.implementation.RxGatewayStoreModel.lambda$toDocumentServiceResponse$0(RxGatewayStoreModel.java:352) at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106) at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:74) at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:200) at reactor.core.publisher.FluxHandle$HandleSubscriber.onNext(FluxHandle.java:126) at reactor.core.publisher.FluxMap$MapConditionalSubscriber.onNext(FluxMap.java:224) at reactor.core.publisher.FluxDoFinally$DoFinallySubscriber.onNext(FluxDoFinally.java:113) at reactor.core.publisher.FluxHandleFuseable$HandleFuseableSubscriber.onNext(FluxHandleFuseable.java:191) at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107) at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1839) at reactor.core.publisher.MonoCollectList$MonoCollectListSubscriber.onComplete(MonoCollectList.java:129) at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:260) at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:144) at reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:413) at reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:424) at reactor.netty.channel.ChannelOperations.terminate(ChannelOperations.java:478) at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:712) at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:113) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:318) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1383) at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1246) at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1295) at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:529) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:468) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.kqueue.AbstractKQueueStreamChannel$KQueueStreamUnsafe.readReady(AbstractKQueueStreamChannel.java:544) at io.netty.channel.kqueue.AbstractKQueueChannel$AbstractKQueueUnsafe.readReady(AbstractKQueueChannel.java:387) at io.netty.channel.kqueue.KQueueEventLoop.processReady(KQueueEventLoop.java:213) at io.netty.channel.kqueue.KQueueEventLoop.run(KQueueEventLoop.java:291) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:834) Suppressed: java.lang.Exception: #block terminated with an error at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99) at reactor.core.publisher.Mono.block(Mono.java:1742) at com.azure.cosmos.CosmosDatabase.blockContainerResponse(CosmosDatabase.java:289) at com.azure.cosmos.CosmosDatabase.createContainerIfNotExists(CosmosDatabase.java:240)
`
To Reproduce Steps to reproduce the behavior:
- Just create a cosmos account with "Azure Cosmos DB for MongoDB" and "Server version: 4.2".
- Run the code above
Expected behavior Create container with specified partition key.
Setup (please complete the following information):
- OS: Macos
- IDE: IntelliJ
- Library/Libraries: com.azure:azure-cosmos:4.47.0
- Java version: 11.0.18
- App Server/Environment: Tomcat
- Frameworks: Spring Boot 2.7.13
Additional context
Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [x] Bug Description Added
- [x] Repro Steps Added
- [x] Setup information Added
Above code tries to use the Cosmos DB Data Plane SDK for NoSql to create a container for a Cosmos DB Mongo Api account. That cannot work - you would need to use the OSS Mongo driver or the ARM SDK - see https://github.com/Azure/azure-sdk-for-java/blob/16de7c69c9aa3443aa8e256e3f4fa6f06974bd90/sdk/resourcemanager/azure-resourcemanager-cosmos/src/main/java/com/azure/resourcemanager/cosmos/fluent/MongoDBResourcesClient.java#L937-L958
Hi @FabianMeiswinkel,
Many thanks for your comment.
I found a workaround with escape partition key "/country" as "/'$v'/country/'$v'", the code looks like
String endPoint = "https://" + mongoDBAccount + ".documents.azure.com:443/";
CosmosClient client = new CosmosClientBuilder()
.endpoint(endPoint)
.key(mongoDBKey)
.buildClient();
CosmosDatabase cosmosDatabase = client.getDatabase(mongoDBDatabase);
cosmosDatabase.createContainerIfNotExists("test","/'$v'/country/'$v'");
Is there any side effect for this way?
Using the NoSql SDK against a Mongo Api account is unsupported. And for newly created Mongo Api accounts it will even be blocked - for existing accounts it might still work (like at least the connection is not getting rejected) - but definitely not something I would recommend using except if this is a one-off.