关于 Golang 的 Mongodb 驱动程序的 Mongodb 查询

Posted

技术标签:

【中文标题】关于 Golang 的 Mongodb 驱动程序的 Mongodb 查询【英文标题】:Mongo DB query on Mongo DB driver for Golang 【发布时间】:2021-02-09 07:38:11 【问题描述】:

我需要比较我的 mongo db 数据库上的两个字段,这就是那个查询

db.characters.find($expr:$eq:["$currentLv", "$maxLv"])

如何在 mongo db driver for golang (mgo) 上进行这样的查询

【问题讨论】:

【参考方案1】:

我不会写如何实例化与 mongodb 的连接,如果你不知道,here is the link。

您的查询将是这样的:

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

filter := bson.M
    "$expr": bson.M
        "$eq": []string"$currentLv", "$maxLv",
    ,


_, _ = db.Database("dbname").Collection("collection").Find(ctx, filter, options.Find())

【讨论】:

以上是关于关于 Golang 的 Mongodb 驱动程序的 Mongodb 查询的主要内容,如果未能解决你的问题,请参考以下文章