[GODRIVER-1019] why insert duplicate document close the client? Created: 05/May/19  Updated: 27/Oct/23  Resolved: 20/May/19

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

Type: Task Priority: Major - P3
Reporter: daniel yang 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:

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\apple\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\gowork\
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\apple\AppData\Local\Temp\go-build881616438=/tmp/go-build -gno-record-gcc-switches



 Description   

After I create an unique index for a field of a document, I insert a duplicate document into mongodb, when using mgo it reports duperr, but user this driver, it reports "client is disconnected" and it disconnects from server either.

 

IMO, this error should not close the connection to the mongodb server, but just report/return an proper error.

 

 

mongo.InitMongo()
client := mongo.GetClient()
c := client.Database("api-server").Collection("users")
var user = bson.M{"name":"bob","age":12} //duplicate document
r, err := c.InsertOne(nil, user)
if err != nil {
   println(err.Error())  //reports client is disconnected
}
// test if really disconnected
var result bson.M
 err=c.FindOne(nil, bson.M{"name":"bob"}).Decode(&result)
if err != nil {
   glog.Errorln(err) // really disconnected
   return
}
 
 println(result["age"])

 



 Comments   
Comment by daniel yang [ 18/May/19 ]

Thanks for reply, please close it.

I found that there is a defer close in the init function for mongodb client, resolved by remove it .

Comment by Divjot Arora (Inactive) [ 06/May/19 ]

yyq Can you confirm that you are calling client.Connect before calling InsertOne? The GoDoc for the driver contains an example of this: https://godoc.org/go.mongodb.org/mongo-driver/mongo.

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