[GODRIVER-1649] Cannot create namespace in multi-document transaction Created: 13/Jun/20  Updated: 27/Oct/23  Resolved: 16/Jun/20

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Muhammad Only Assignee: Divjot Arora (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MacOs, latest mongo-community



 Description   

I am using GoDriver mongo in GraphQL, trying to do transaction for `insertOne` but always got error like this

(NamespaceNotFound) Cannot create namespace production.people in multi-document transaction.

I do create mongo replica as well,
const MONGODB_URI = "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs"
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(MONGODB_URI))
if err != nil {
panic(err)
}

db := client.Database("production")
defer db.Client().Disconnect(ctx)
col := db.Collection("people")

// transaction
err = db.Client().UseSession(ctx, func(sessionContext mongo.SessionContext) error {
err := sessionContext.StartTransaction()
if err != nil

{ fmt.Println(err) return err }

_, err = col.InsertOne(sessionContext, req)
if err != nil {
sessionContext.AbortTransaction(sessionContext)
fmt.Println(err)
return err
} else {
sessionContext.CommitTransaction(sessionContext)
}
return nil
})

if err != nil {
return nil, err

 

I have follow the instructions of this question in Stackoverflow and I have tried also ways from this article mongodb developer

but not lucky and always got issue like above, I have follow on your test also , still the same, any idea? or my code has something wrong?? if no, is this a bug?

Thank you

{{}}



 Comments   
Comment by Divjot Arora (Inactive) [ 16/Jun/20 ]

Hi myhoomac@hotmail.com,

Thank you for the detailed report! This is actually a server limitation as the MongoDB server currently does not allow collections and indexes to be created during a transaction. Running InsertOne on a collection that doesn't exist on the server causes the server to implicitly create the collection, which fails with the error you're seeing. This will be fixed in MongoDB 4.4.0.

In the meantime, you can create the collection before running the transaction. This can be done by inserting a document into the collection before the UseSession call or by running the create command:

db := client.Database("production")
col := db.Collection("people")
 
// Run create command
createCmd := bson.D{{"create", col.Name()}}
err := db.RunCommand(ctx, createCmd)
// handle err
 
// existing UseSession stuff

Version 1.4.0 of the driver will introduce a Database.CreateCollection function so you can explicitly create collections without needing to use RunCommand.

I'm going to close this ticket, but feel free to leave a comment if you have any further questions!

– Divjot 

Generated at Thu Feb 08 08:36:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.