Cannot get mongodb entry from ObjectId in golang

I am trying to get the mongodb ObjectId entry using the following code, but not getting not found err.Error()

Below is a sample of my mongo collection

{ "_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 }

Below is my model

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"` 
} 

I want to search this entry by object 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
}

But when I try to get all the collection entries in the following code, it works fine

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

enter image description here

I also check the following SO Q & A, but didn't work

Mongodb request from golang using _id stored in array

How to find by id in golang and mongodb

Cannot get "_id" using mgo with golang

+4
source share
1 answer

Any of the above comments or answers cannot solve the problem, so what I do is

1) .

    i := bson.NewObjectId()

    rhinoJobs.id = i

    err = coll.Insert(rhinoJobs)

2) Id

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
}

, - .

Mongo ObjectId ?

0

Source: https://habr.com/ru/post/1690428/


All Articles