简体   繁体   中英

Using mongodb native function via mongo-go-driver

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM