通过mongo-go-driver使用mongodb本机函数


正如标题所示,如何使用mongo-go-driver来执行mongodb的本机函数,如ISODate().getTime()?我找不到好的文档,甚至在mongo的官方文档中也找不到这样的文档。

例如,查询

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

大致翻译为

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

但是通过这种方式,最后看到的值将不是mongo中的UNIX时间戳,而是文字字符串"ISODate().getTime()“

注意:对于虚拟情况,最好不要在应用程序级别创建时间戳,因此需要使用DB的函数

转载请注明出处:http://www.shenkehuoyun.com/article/20230526/1234379.html