MongoDB 不同的查询和 $in with Go
MongoDB distinct query and $in with Go
我在使用 MongoDB 中的不同查询时遇到问题。
我可以写成Mongoshell,可以用,但是我不知道怎么用Go代码实现。
这是我的 Mongo shell 代码
db.getCollection('company_role_function').distinct("rolecode", {rolecode : {
$in: ['DHBK_ROLE_01','DHBK_ROLE_03' ] },productid:'IOT_Platform'
})
这是我的 Go 代码
1.profile.go
type CompanyRoleFunction struct {
Rolecode string `json:"rolecode"`
Productid string `json:"productid"`
Functioncode string `json:"functioncode"`
Comid string `json:"comid"`
}
repository.go
package repository
import "bitbucket.org/cloud-platform/vnpt-sso-usermgnt/model"
type IProfileRepository interface {
FindRoleByUserProduct(string) (*model.CompanyRoleFunction, error)
}
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
}
在 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)
}
我在使用 MongoDB 中的不同查询时遇到问题。
我可以写成Mongoshell,可以用,但是我不知道怎么用Go代码实现。
这是我的 Mongo shell 代码
db.getCollection('company_role_function').distinct("rolecode", {rolecode : {
$in: ['DHBK_ROLE_01','DHBK_ROLE_03' ] },productid:'IOT_Platform'
})
这是我的 Go 代码
1.profile.go
type CompanyRoleFunction struct {
Rolecode string `json:"rolecode"`
Productid string `json:"productid"`
Functioncode string `json:"functioncode"`
Comid string `json:"comid"`
}
repository.go
package repository import "bitbucket.org/cloud-platform/vnpt-sso-usermgnt/model" type IProfileRepository interface { FindRoleByUserProduct(string) (*model.CompanyRoleFunction, error) }
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 }
在 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)
}