简体   繁体   English

对 Golang 的 Mongo DB 驱动程序的 Mongo DB 查询

[英]Mongo DB query on Mongo DB driver for Golang

I need to compare two fields on my mongo db database, and this is the query for that我需要比较 mongo db 数据库上的两个字段,这是查询

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

How can i query like this on mongo db driver for golang (mgo)我如何在 golang (mgo) 的 mongo db 驱动程序上进行这样的查询

I won't write about how instantiate connection to mongodb, if you didn't know, here is the link .我不会写如何实例化到 mongodb 的连接,如果你不知道,这里是链接

And your query will be something like that:你的查询将是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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