MongoDB独特查询和$in with Go

MongoDB独特查询和$in with Go,go,mgo,mongo-go,Go,Mgo,Mongo Go,我在MongoDB中遇到了不同查询的问题 我可以在MongoShell中编写它,它可以工作,但我不知道如何在Go代码中实现它 这是我的Mongo shell代码 db.getCollection('company_role_function').distinct("rolecode", {rolecode : { $in: ['DHBK_ROLE_01','DHBK_ROLE_03' ] },productid:'IOT_Platform' }) 这是我的密码 1.profile.go

我在MongoDB中遇到了不同查询的问题

我可以在MongoShell中编写它,它可以工作,但我不知道如何在Go代码中实现它

这是我的Mongo shell代码

db.getCollection('company_role_function').distinct("rolecode", {rolecode : {
   $in: ['DHBK_ROLE_01','DHBK_ROLE_03' ] },productid:'IOT_Platform'
})
这是我的密码

1.profile.go

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

package repository
import "bitbucket.org/cloud-platform/vnpt-sso-usermgnt/model"
type IProfileRepository interface {
   FindRoleByUserProduct(string) (*model.CompanyRoleFunction, error)
}
蒙哥乌司机,加油

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
 }

在mgo中尝试使用以下代码进行distinct

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)


}

嗨,先生,这还是不起作用。你的搜索id是什么?我按照你的指南编写代码,替换搜索id=rolecode。但是它不起作用。///func r*ProfileRepositoryMongo FindRoleByUserProduct*model.CompanyRoleFunction,error{var CompanyRoleFunction model.CompanyRoleFunction rolarray:=[]string{DHBK_ROLE_01,DHBK_ROLE_03}err:=r.db.Cr.collection.Findbson.M{search\u id:bson.M{$in:roleArray},productid:IOT\u Platform}.Distinctrolecode,&companyRoleFunction if err!=nil{return nil,err}return&companyRoleFunction,nil}对不起,我弄错了它的角色码,不是search_id,答案更新了,你说你把它替换了,但在你上面的评论中,我仍然可以看到searh_id和行r.db.Cr.collection.Findbson.M{rolecode:bson.M{$in:roleArray},productid:'IOT_Platform'}.Distinctrolecode,&result-->productid应该在和rolarray中?我已经更新了答案,rolarray不需要引号,因为它是一个变量,如果出现语法错误,您需要检查语法错误,我还没有运行代码,从逻辑上讲,这是正确的,您混合了过时的gopkg.in/mgo.v2驱动程序和官方的go.mongodb.org驱动程序。我建议使用官方司机。谢谢@altu,我会听从你的建议。