globalsign / mgo

The MongoDB driver for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GridFS driver doesn't create any index on the files collection

hjr265 opened this issue · comments

According to the GridFS specification, there should exist an index on the files collection (ref):

an index on { filename : 1, uploadDate : 1 } on the files collection
a unique index on { files_id : 1, n : 1 } on the chunks collection

I did a quick scan of the gridfs.go file and it seems the only index that is created in that file is the second one:

mgo/gridfs.go

Line 561 in eeefdec

file.err = file.gfs.Chunks.EnsureIndex(index)

I can confirm that this behavior of having only the second index created is consistent in one of my production setup (which is how I discovered this potential issue). Unless I am missing something obvious (I am sorry if I am), I think this is a deviation from the specification and should be worth correcting.


What version of MongoDB are you using (mongod --version)?

4.0.9

What version of Go are you using (go version)?

go version go1.12.1 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build246555118=/tmp/go-build -gno-record-gcc-switches"

What did you do?

N/A

Can you reproduce the issue on the latest development branch?

The gridfs.go file in the latest development branch also has one call to EnsureIndex and that is on the chunks collection:

mgo/gridfs.go

Line 561 in 909ae7c

file.err = file.gfs.Chunks.EnsureIndex(index)

I can send a pull request for this if you want.

commented

No, when you create file via GridFS(..).Create(....), id will be created. return file, error. you can get id from func (*GridFile) Id

refer to code line 174, below

func (gfs *GridFS) Create(name string) (file *GridFile, err error) {
	file = gfs.newFile()
	file.mode = gfsWriting
	file.wsum = md5.New()
	file.doc = gfsFile{Id: bson.NewObjectId(), ChunkSize: 255 * 1024, Filename: name}
	return
}

@lujiacn Thanks, but I was referring to the creation of database indices, not IDs.