[_ id尝试查找时始终找不到错误

问题描述 投票:0回答:1

我正在使用Go mgo通过_id查找,但始终找不到错误。

我尝试添加2个查询(请参见下面的代码)

  1. 由_id查找(总是找不到错误)
  2. 由其他字段查找(成功),我还打印了它的ID(它与传递给第一个查询的ID相同)>
  3. 我的go.mod在这里

gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8

示例代码在这里

package mongodb

import (
    "fmt"

    "github.com/globalsign/mgo/bson"
    "github.com/google/logger"
    "gitlab.com/kbtg-lab-bc/rekeep/rekeep-service/server/config"
    "gitlab.com/kbtg-lab-bc/rekeep/rekeep-service/server/constant"
    "gitlab.com/kbtg-lab-bc/rekeep/rekeep-service/server/lib/log"
    "gitlab.com/kbtg-lab-bc/rekeep/rekeep-service/server/models"

    "gopkg.in/mgo.v2"
)

func Run(c *config.Config) error {
    ss, _ := mgo.Dial("1.2.3.4:27017")
    cred := mgo.Credential{
        Username: "user",
        Password: "abcd",
    }
    ss.Login(&cred)

    type Temp struct {
        ID bson.ObjectId `json:"id" bson:"_id"`
    }
    tmp := Temp{}

    id := "5dd640599ab5939a39120005"
    err := ss.DB("rekeep").C("merchants").Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&tmp)
    fmt.Println("find by _id: error = ", err)

    err = ss.DB("rekeep").C("merchants").Find(bson.M{"activationCode": "12345678"}).One(&tmp)
    fmt.Println("find by activationCode: error = ", err)
    fmt.Println(tmp.ID.Hex())

    return nil
}

my database

我正在使用Go mgo通过_id查找,但始终找不到错误。我尝试添加2个查询(请参见下面的代码),通过_id查找(总是找不到错误),通过其他字段(成功)查找,我还打印其...

go mgo
1个回答
0
投票

在与Facebook组上的golang开发人员讨论后,我找到了原因。

© www.soinside.com 2019 - 2024. All rights reserved.