func makeTimedObjectId(t time.Time) bson.ObjectId {
oid := bson.NewObjectId().Hex()
nid := fmt.Sprintf("%x%s", t.Unix(), oid[8:])
return bson.ObjectIdHex(nid)
}
func ObjectIdTime(t time.Time) ObjectId {
var b [12]byte
// Timestamp, 4 bytes, big endian
binary.BigEndian.PutUint32(b[:], uint32(t.Unix()))
// Machine, first 3 bytes of md5(hostname)
b[4] = machineId[0]
b[5] = machineId[1]
b[6] = machineId[2]
// Pid, 2 bytes, specs don't specify endianness, but we use big endian.
pid := os.Getpid()
b[7] = byte(pid >> 8)
b[8] = byte(pid)
// Increment, 3 bytes, big endian
i := atomic.AddUint32(&objectIdCounter, 1)
b[9] = byte(i >> 16)
b[10] = byte(i >> 8)
b[11] = byte(i)
return ObjectId(b[:])
}
//change NewObjectid
func NewObjectId() ObjectId {
return ObjectIdTime(time.Now())
}