Mongo DB query on Mongo DB driver for Golang

350 Views Asked by At

I need to compare two fields on my mongo db database, and this is the query for that

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

How can i query like this on mongo db driver for golang (mgo)

1

There are 1 best solutions below

0
Mully On

I won't write about how instantiate connection to mongodb, if you didn't know, here is the link.

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