On Swapping Mongo Driver

10/03/2019 17:13 GMT

I recently swap Mongo DB driver from a third party to the official driver, the process mostly went smoothly because I was well disciplined in writing high quality code and sticking to good practice otherwise it would of took me a lot longer to complete.

I did have a few issues along the way.

Refactoring Gridfs Collections

The third party Mongo DB drivers somewhat conforms to Gridfs specification, the third party allows you to specify the collection for files and chunks, which is nice but the official driver does not allow you to specify the collection instead you have to specify the bucket name and the default value is ‘fs’ and I left it like that.

The naming convention for the collection are ‘bucketName.files’ and ‘bucketName.chunks’, as I mentioned earlier I left it at the default so I had to rename the collections to ‘fs.files’ and ‘fs.chunks’ when I did that live I had to deploy immediately so there is little downtime.

I also had a few data type mismatch so I had to write a script and execute it’s manually in Robo 3T, replacing NumberInt (int32) to NumberLong (int64) and that fixed the mismatch.

db.getCollection('fs.files').find({}).forEach(function(x) {
        x.chunkSize = new NumberLong(x.chunkSize);
        x.length = new NumberLong(x.length);
        db.getCollection('fs.files').save(x);
});

The third party allowed access to the metadata, but the official did not, so I had to create a clone of ‘fs.files’ and called it ‘filesMeta’ so can still access and update the metaData; also I kept ID 1:1 with each other.

Data type mismatch with the key of the map

The third party allows you to use any data type as the key, the official only allows string; so I had to change the data type of the key from int to string and problem solved. It’s not too bad after all I did attached a method to the map, all I had to do is convert int to string in that method. I’m using Golang, it’s strongly typed, I had to do explicit conversion, but I don’t mind, I like clarity, clarity is always good.

// Before
type PageCollection struct {
	Ref        string       `bson:"Ref"`
	Collection map[int]Page `bson:"Collection"`
}

func (p PageCollection) GetPage(pageNumber int) Page {
	page, found := p.Collection[pageNumber]
	checkIfFound(found)

	return page
}

// After
type PageCollection struct {
	Ref        string          `bson:"Ref"`
	Collection map[string]Page `bson:"Collection"`
}

func (p PageCollection) GetPage(pageNumber int) Page {
	page, found := p.Collection[fmt.Sprint(pageNumber)]
	checkIfFound(found)

	return page
}

The most complained about language is JavaScript and it’s loosely typed which is the opposite of strongly typed and trust me the data type mismatch take longer to figure out in JS than it’s does with Go!

| |