0

I'm having trouble with the distinct query in MongoDB.

I can write it in Mongo shell, it works but I don't know how to implement it in Go code.

Here is my Mongo shell code

db.getCollection('company_role_function').distinct("rolecode", {rolecode : {
   $in: ['DHBK_ROLE_01','DHBK_ROLE_03' ] },productid:'IOT_Platform'
})

And here is my Go code

1.profile.go

    type CompanyRoleFunction struct {
        Rolecode     string `json:"rolecode"`
        Productid    string `json:"productid"`
        Functioncode string `json:"functioncode"`
        Comid        string `json:"comid"`
     }
  1. repository.go

    package repository
    import "bitbucket.org/cloud-platform/vnpt-sso-usermgnt/model"
    type IProfileRepository interface {
       FindRoleByUserProduct(string) (*model.CompanyRoleFunction, error)
    }
    
    1. mongo_driver.go

      package repository
      import (
          "bitbucket.org/cloud-platform/vnpt-sso-usermgnt/model"
          "go.mongodb.org/mongo-driver/bson"
          "gopkg.in/mgo.v2"
      )
      type ProfileRepositoryMongo struct {
        db         *mgo.Database
        collection string
       }
      
       func NewProfileRepositoryMongo(db *mgo.Database, collection string) *ProfileRepositoryMongo {
          return &ProfileRepositoryMongo{
          db:         db,
          collection: collection,
         }
      }
      //I HAVE TROUBLE HERE
       func (r *ProfileRepositoryMongo) FindRoleByUserProduct(rolecode arr[]string)  (*model.CompanyRoleFunction, error) {
        var companyRoleFunction model.CompanyRoleFunction
         //I HAVE TROUBLE HERE
        err := r.db.C(r.collection).Find(bson.M{"username": username}).One(&companyRoleFunction)
         //I HAVE TROUBLE HERE
        if err != nil {
            return nil, err
         }
        return &companyRoleFunction, nil
       }
      
2
  • You're mixing the outdated gopkg.in/mgo.v2 driver and the official go.mongodb.org driver. I'd suggest using the official driver. pkg.go.dev/go.mongodb.org/mongo-driver/… . Commented May 16, 2020 at 2:23
  • Thank @altu, I will follow your suggest. Commented May 16, 2020 at 2:56

1 Answer 1

1

Try the below code for distinct in mgo

package main

import (
"context"
"fmt"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2"
)

type Result struct {
Rolecode string `json:"rolecode"`
Productid string `json:"productid"`
Functioncode string `json:"functioncode"`
Comid string `json:"comid"`
}
type Results []Result

func main() {
//delete1("GV_BMVT")
//update("GV_BMVT")
check()
}

func check() {
session, err := mgo.Dial("mongodb://casuser:[email protected]:27017/users")
if err != nil {
panic(err)
}
c := session.DB("users").C("company_role_function")
results := []string{}

roleArray := []string{"DHBK_ROLE_01,", "DHBK_ROLE_03"}
err = c.Find(bson.M{"rolecode": bson.M{"$in": roleArray}, "productid": "IOT_Platform"}).Distinct("rolecode", &results)
if err != nil {
panic(err)
}
fmt.Println(results)


}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Sir, this still doesn't work. What is your "search_id"?
I write code follow your guide, replace search_id = rolecode. But it doesn't work.//// func (r *ProfileRepositoryMongo) FindRoleByUserProduct() (*model.CompanyRoleFunction, error) { var companyRoleFunction model.CompanyRoleFunction roleArray := []string{"DHBK_ROLE_01", "DHBK_ROLE_03"} err := r.db.C(r.collection).Find(bson.M{"search_id": bson.M{"$in": roleArray}, "productid": "IOT_Platform"}).Distinct("rolecode", &companyRoleFunction) if err != nil { return nil, err } return &companyRoleFunction, nil }
Sorry my mistake its rolecode, not search_id, answer updated You said you replaced it, but in your above comment I can still see searh_id
And in line "r.db.C(r.collection).Find(bson.M{"rolecode": bson.M{"$in": roleArray}, productid:'IOT_Platform'}).Distinct("rolecode",&result)" --->productid should in " " and roleArray?
I have updated the answer, roleArray doesn't need quotes as it is a variable, you need to check the syntax error if any comes, I haven't run the code, logically it is correct
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.