gpt4 book ai didi

mongodb - 在golang中无法通过ObjectId获取mongodb记录

转载 作者:IT老高 更新时间:2023-10-28 13:28:26 25 4
gpt4 key购买 nike

我尝试使用以下代码通过 ObjectId 获取 mongodb 记录,但通过 err.Error()

不断获取 not found

以下是我的 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

最佳答案

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

我基本上将 globalsign 中的 mgo 与 gopkg.in 中的旧 bson 一起使用。请检查您的依赖关系!

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

这是错误的!

应该是:

"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"

关于mongodb - 在golang中无法通过ObjectId获取mongodb记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47712710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com