Using mongodb native function via mongo-go-driver

152 Views Asked by At

Just as the title, how does one use mongo-go-driver to execute mongodb's native function, such as ISODate().getTime()? I can't find a good docs, even in mongo's official docs, to do this.

For example, query

db.coll.update({key: 'random-id'}, {$set: {last_seen: ISODate().getTime()}})

Roughly translate to

coll.updateOne(
    ctx,
    bson.D{
       bson.E{Key: "key", Value: "random-id"},
    },
    bson.D{
       bson.E{Key: "last_seen", Value: "ISODate().getTime()"},
    }
)

but that way, the last seen value wont be a UNIX timestamp in mongo, but literal string "ISODate().getTime()"

Notes: For the dummy case, preferably not have to create the timestamp in the application level, so need to use DB's function

1

There are 1 best solutions below

3
On

This is not possible. The code you are trying to use is mongo shell code. You need to construct the command using Go because you are programming in Go, not in mongo shell.