Golang/mgo: How can I ask MongoDB to use current time in a field?

Sebastián Grignoli picture Sebastián Grignoli · Aug 17, 2012 · Viewed 21.3k times · Source

I have this struct that matches the types of a MongoDB collection I'm using:

type AppInstance struct {
    Id bson.ObjectId "_id,omitempty"
    Url string
    Priority int
    LastSeen string
}

I want the LastSeen field to hold the time of the last interaction with that particular app. So, the app registers itself setting current time (as a string).

What I would like is Mongo to dynamically set its own current time into that field when it inserts, just like MySQL's NOW() function would do.

I have this helper function:

func mongoNow() bson.JavaScript {
    return bson.JavaScript{Code: 
         "return (new Date()).ISODate('YYYY-MM-DD hh:mm:ss');"}
}

And I tried this:

c := mongoSession.DB("myapp").C("instances")
rand.Seed(time.Now().UnixNano())
err := c.Insert(
   struct{Id, Serial, Priority, Url, LastSeen interface{}}{ 
      Id: bson.NewObjectId(), 
      Url: getInformedHost() + ":" + getRunningPortString(), 
      Priority: rand.Int(), 
      LastSeen: mongoNow() }
)
checkError(err, "Could not register on MongoDB server.", 3)

the LastSeen field gets stored as a script instead of evaluated:

[_id] => MongoId Object (
    [$id] => 502d6f984eaead30a134fa10
)
[id] => MongoId Object (
    [$id] => 502d6f98aa443e0ffd000001
)
[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode Object (
    [code] => (new Date()).ISODate('YYYY-MM-DD hh:mm:ss')
    [scope] => Array (
    )
)

So, I think there are to questions:

First, how can I insert the current time?

Second, how can I get some javascript evaluated instead of inserted?

The answer to the second one could be enough to answer the first one, but it might as well not be.

Answer

Zippo picture Zippo · Aug 17, 2012

Don't store time as string. mgo supports time.Time which is like a Date object in Javascript:

type Event struct {
    Id    bson.ObjectId "_id,omitempty"
    Which string
    Date  time.Time
}

Insert an event that happened now:

e := Event{
    Which: "first event",
    Date: time.Now(),
}
c.Insert(e)