正如标题所示,如何使用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的函数
发布于 2020-10-15 22:37:00
这是不可能的。您尝试使用的代码是mongo shell代码。你需要使用Go来构造命令,因为你是在Go中编程,而不是在mongo shell中。
https://stackoverflow.com/questions/64370074
复制相似问题