question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma with CosmosDB, MongoDB connector has error for sharded collections

See original GitHub issue

Discussed in https://github.com/prisma/prisma/discussions/9544

<div type='discussions-op-text'>

Originally posted by dohaicuong October 1, 2021 Hi Prisma Team! I just got this error when setup Prisma with MongoDB on Azure CosmosDB and create a sharded collection

Invalid `prisma.user.create()` invocation:\n\n\n  Error occurred during query execution:\nConnectorError(ConnectorError {
user_facing_error: None, kind: RawError { 
code: \"unknown\", message: \"Command failed (50841): Multi-document transactions cannot be run in a sharded collection)\" 
} })

Versions

  • Node: v14.17.4
  • OS: macOs
  • Prisma client: 3.1.1
  • Database: MongoDB 4.0 on Azure CosmosDB

Schema

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongoDb"]
}

model User {
  id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
  name String
}

MongoDB setup

  • Create mongoDB 4.0 on cosmosDB
  • Create new DB and User collection with shard key

Connection string

mongodb://user:pass@url/db_name?ssl=true
&connectTimeoutMS=5000
&maxPoolSize=50
&replicaSet=globaldb
&retrywrites=false
&maxIdleTimeMS=120000
&appName=@app_name@

Prisma command

const newUser = await prisma.user.create({
  data: { name: 'name' }
})

Errors

Invalid `prisma.user.create()` invocation:\n\n\n  Error occurred during query execution:\nConnectorError(ConnectorError { user_facing_error: None, kind: RawError { code: \"unknown\", message: \"Command failed (50841): Multi-document transactions cannot be run in a sharded collection)\" } })
```</div>

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
janpiocommented, Feb 19, 2022

Unfortunately the MongoDB API of Azure CosmosDB is stuck at version 4.0: https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb/mongodb-introduction#how-the-api-works @matthewmueller - but this problem is CosmosDB specific, not caused by MongoDB 4.0 (see below)


I can now also reproduce a close error with a simple CosmosDB with MongoDB 4.0 API via the CosmosDB Emulator:

$ ts-node ./script.ts
PrismaClientUnknownRequestError: 
Invalid `client.user.create()` invocation in
D:\a\e2e-tests\e2e-tests\databases-windows\mongodb-azure-cosmosdb-emulator\script.ts:9:21

   6 async function main() {
   7 
   8   // On CosmosDB the first delete will fail if you do not make sure the database and collection actually exist, so creating so dummy data first
→  9   await client.user.create(
  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (OperationNotSupportedInTransaction): Command insert failed : can not create collection in a multi-document transaction)" } })
    at cb (D:\a\e2e-tests\e2e-tests\databases-windows\mongodb-azure-cosmosdb-emulator\node_modules\@prisma\client\runtime\index.js:39807:17) {
  clientVersion: '3.10.0-dev.54'
}

Knowing these concepts now, I could also find the documention on the CosmosDB side for this:

Transactions Multi-document transactions are supported within an unsharded collection. Multi-document transactions are not supported across collections or in sharded collections. The timeout for transactions is a fixed 5 seconds.

https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb/feature-support-40#transactions

So:

  • Prisma is using “multi-document transactions” by default to create a new collection.
  • CosmosDB MongoDB does not support that for “across collections or in sharded collections”.

The exact error you got @dohaicuong I indeed only get when I create a collection via the web UI and explicitly enable sharing on it:

$ ts-node ./script.ts
success
PrismaClientUnknownRequestError: 
Invalid `client.test.create()` invocation in
/home/runner/work/e2e-tests/e2e-tests/databases/mongodb-azure-cosmosdb/script.ts:38:21

  35 
  36 console.log('success')
  37 
→ 38 await client.test.create(
  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (50841): Multi-document transactions cannot be run in a sharded collection)" } })
    at cb (/home/runner/work/e2e-tests/e2e-tests/databases/mongodb-azure-cosmosdb/node_modules/@prisma/client/runtime/index.js:39807:17) {
  clientVersion: '3.10.0-dev.66'
}

image

I assume that is also what you did?

1reaction
janpiocommented, Mar 16, 2022

Side note: We also do not know yet if we support Shard Clusters of normal MongoDB: https://github.com/prisma/prisma/issues/12380

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot common errors in Azure Cosmos DB's API for ...
Code Error Description 13 Unauthorized The request lacks the permissions to complete. 67 CannotCreateIndex The request to create an index can't be completed. 115 CommandNotSupported The...
Read more >
MongoDB database connector - Prisma
The MongoDB data source connector connects Prisma to a hosted MongoDB instance ... has no replica set configured, you can experience an error...
Read more >
Azure Cosmos DB Pitch Deck L400 - WinDays.hr
Azure Cosmos DB uz MongoDB API i SQL API. Ivan Pranjić. Blink d.o.o. ... Cons: Fixed schemas help prevent errors ... The unit...
Read more >
expected connection string to start with mongodb:/ or mongodb+srv ...
Describe the bug When trying to use Strapi with the mongoose connector and Azure CosmosDB (MongoDB API 3.6), Strapi crashes with an error....
Read more >
Error in connector: missing field `info` - Prisma Introspection ...
I have created an Express, Apollo GraphQL server which accepts requests and returns data from a MongoDB database via Prisma.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found