In each data system, we need a way to uniquely identify an object, there are many solutions, for simple relational database table for example MySQL, there is an auto increment count that gives each row an id. In Git version control system, each object, commit has an ID, the UUID is widely used on Windows system.

A MongoDB document id is similar to UUID but has some differences. The structure of a MongoDB document id is

4 byte seconds since epoch
3 byte machine id
2 byte process id
3 byte counter

Its highly unlikely to generate duplicated document id in this way. Its a safe way to allocate unique identity for a data object system.

To generate MongoDB id with Clojure, we need the Monger, a Clojure client for MongoDB. Here is the Lein dependency.

[com.novemberain/monger "3.0.0-rc2"]

The following code will generate and print a new MongoDB document id.

(defn mid []
  (print (.toString (org.bson.types.ObjectId.)))