Golang mgo create objectid from a hexadecimal string example

19 May, 2016

Storing a string that holds the hexadecimal string of a objectID is done by using the bson package with function 'objectIdHex()'.

Golang mgo findAndModify auto increment id tutorial

17 May, 2016

Here is a brief explanation for the 'findAndModify()' equivalent for mgo. Instead of 'findAndModify()' we will use 'mgo.Find().Apply()'. I will demonstrate how to use auto incremented ids instead of random unique generated IDs for documents, since mongdb does not have a native way of doing this (because MongoDB is not a relational database).

It work's like this, we create a collection, insert a document in that collection that contains the last generated ID. Every time we call 'findAndModify()', mongoDB will increment the value and we get that new value back, which you should use to insert a new document with.