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
       }
      
DallaRosa
  • 5,737
  • 2
  • 35
  • 53
ThanhLam112358
  • 878
  • 1
  • 20
  • 51
  • You're mixing the outdated gopkg.in/mgo.v2 driver and the official go.mongodb.org driver. I'd suggest using the official driver. https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc#Collection.Distinct . –  May 16 '20 at 02:23
  • Thank @altu, I will follow your suggest. – ThanhLam112358 May 16 '20 at 02:56

1 Answers1

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:Mellon@222.255.102.145: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)


}
Puneet Singh
  • 3,477
  • 1
  • 26
  • 39
  • Hi Sir, this still doesn't work. What is your "search_id"? – ThanhLam112358 May 15 '20 at 08:49
  • 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 } – ThanhLam112358 May 15 '20 at 08:49
  • 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 – Puneet Singh May 15 '20 at 09:04
  • 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? – ThanhLam112358 May 15 '20 at 09:15
  • 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 – Puneet Singh May 15 '20 at 09:23
  • hi Sir. I have added my code below base on your guide. But it show empty array []. Please take a look. – ThanhLam112358 May 15 '20 at 16:32
  • And I found one point. When I print variable roleArray := []string{"DHBK_ROLE_01", "DHBK_ROLE_03"} ----> roArray = [DHBK_ROLE_01 DHBK_ROLE_03]. If is different from ['DHBK_ROLE_01','DHBK_ROLE_03' ] in my Mongo shell. – ThanhLam112358 May 15 '20 at 16:52
  • Just add sample mongodb row as well in question if u can, will run complete code on my end – Puneet Singh May 15 '20 at 16:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213956/discussion-between-nistelrooy41001662-and-puneet-singh). – ThanhLam112358 May 15 '20 at 16:59