在golang

时间:2017-12-08 10:44:54

标签: mongodb go bson

我试图通过使用以下代码获取ObjectId的mongodb记录,但是err.Error()继续找不到

以下是我的mongo收集样本

{ "_id" : ObjectId("5a2a75f777e864d018131a59"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }
{ "_id" : ObjectId("5a2a75f877e864d018131a5b"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }
{ "_id" : ObjectId("5a2a75fa77e864d018131a5d"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }

以下是我的模特

type RhinoJobs struct { 
ID                bson.ObjectId  `db:"id" json:"id" bson:"_id"` 
CallDate          string  `db:"call_date" json:"callDate" bson:"callDate"` 
Time              string  `db:"time" json:"time" bson:"time"` 
CallType          string  `db:"call_type" json:"callType" bson:"callType"` 
Position          string  `db:"position" json:"position" bson:"position"` 
Description       string  `db:"description" json:"description" bson:"description"` 
Qty               int     `db:"qty" json:"qty" bson:"qty"` 
EstimatedDuration float64 `db:"estimated_duration" json:"estimatedDuration" bson:"estimatedDuration"` 
EstimatedOvertime float64 `db:"estimated_overtime" json:"estimatedOvertime" bson:"estimatedOvertime"` 
Rate              float64 `db:"rate" json:"rate" bson:"rate"` 
LaborExtension    float64 `db:"labor_extension" json:"laborExtension" bson:"laborExtension"` 
} 

我想按对象ID

搜索这些记录
"gopkg.in/mgo.v2"

func GetMongoSession() (*mgo.Session, error) {
    if mgoSession == nil {
        var err error
        mgoSession, err = mgo.Dial(mongoConnectionUrl)
        if err != nil {
            return nil, err
        }
    }
    return mgoSession.Clone(), nil
}



func GetJobByID(objID string) (models.RhinoJobs, error) {
    var job models.RhinoJobs
    s, err := commons.GetMongoSession()
    if err != nil {
        errMsg := "error occurred while creating mongoDB connection stack:" + err.Error()
        print(err.Error())
        return job, errors.New(errMsg)
    }
    defer s.Close()
    err = s.DB("rhino").C("jobs").FindId(bson.ObjectIdHex("5a2a75f777e864d018131a59")).One(&job)
    if err != nil {
        print(err.Error())
        errMsg := "error occurred while getting data :" + err.Error()
        return job, errors.New(errMsg)
    }
    return job, nil
}

但是当我尝试通过以下代码获取所有收集记录时,它可以正常工作

err := s.DB("rhino").C("jobs").Find(nil).All(&jobs)

enter image description here

我也检查了SO Q& A但是没有工作

Querying mongodb from golang using the _id stored in an array

How to find by id in golang and mongodb

Cannot retrieve "_id" value using mgo with golang

2 个答案:

答案 0 :(得分:4)

我遇到了完全相同的问题,我的问题是我最近切换到新的mgo fork(正在积极开发)并且我混合了依赖项。

我基本上使用来自globalsign TOGETHER的HiDPI和来自gopkg.in的旧mgo。请检查您的依赖项!

bson

这是错的!!

相反它应该是:

"github.com/globalsign/mgo"
"gopkg.in/mgo.v2/bson"

答案 1 :(得分:0)

上述任何评论或答案都无法解决问题,我所做的就是,

1)添加自定义生成的objectId进行记录。

    i := bson.NewObjectId()

    rhinoJobs.id = i

    err = coll.Insert(rhinoJobs)

2)按自定义objectId搜索

func GetJobByID(objID string) (models.RhinoJobs, error) {
    var job models.RhinoJobs
    s, err := commons.GetMongoSession()
    if err != nil {
        errMsg := "error occurred while creating mongoDB connection stack:" + err.Error()
        print(err.Error())
        return job, errors.New(errMsg)
    }
    defer s.Close()
    err = s.DB("rhino").C("jobs").FindId(bson.ObjectIdHex(objID)).One(&job)
    if err != nil {
        print(err.Error())
        errMsg := "error occurred while getting data :" + err.Error()
        return job, errors.New(errMsg)
    }
    return job, nil
}

认为这可能会对将来有所帮助。

有用的链接

Possibility of duplicate Mongo ObjectId's being generated in two different collections?