Gogo的Mongo DB驱动程序上的Mongo DB查询

时间:2020-10-27 02:12:17

标签: mongodb go mgo

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

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

我该如何在mongo db驱动程序上查询golang(mgo)

1 个答案:

答案 0 :(得分:0)

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

您的查询将是这样的:

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())