【问题标题】:Unable to Decode the ObjectId SubValue from MongoDB results in Golang无法从 Golang 中的 MongoDB 结果中解码 ObjectId SubValue
【发布时间】:2019-08-22 01:08:16
【问题描述】:

我正在使用MongoDb Go Driver,但无法从我的结构中解码的 JSON 中获取 ObjectId 子值。

注意:我使用的库/API 与 this question 不同,因此请不要将其标记为重复。

import (
    "net/http"
    "github.com/go-chi/chi"
    "encoding/json"
    "time"
    "context"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "fmt"
)

我有一种这样的结构来处理结果

type Contact struct {
    Id  struct {
        ObjId   string  `json:"$oid"`
    } `json:"_id"`
    Name    string `json:"name"`
    Email   string `json:"email"`
    Health  struct {
        Weight  int `json:"weight"`
        Height  int `json:"height"`
    } `json:"health"`    
}

然后我去检索这样的联系人:

var contacts []Contact
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
    panic(err)
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
    var contact Contact
    fmt.Println(cursor)
    cursor.Decode(&contact)
    contacts = append(contacts, contact)
}
if err := cursor.Err(); err != nil {
    panic(err)
}
// I want to do more with the contacts, but .Id is empty :-(
fmt.Println(contacts)

"health" 的子字段完全按照应有的方式显示,但由于某种原因,结果中 "_id" 部分的子字段无处可寻。有人可以帮我解决这个问题吗? ?

来自数据库的 JSON 响应是这样的,出于某种原因,我能够获得 health 字段的子字段,但不能获得 _id 字段的子字段。为什么不?

数据库的原始 JSON 响应

[{
    "_id": { 
        "$obj": "5c601648ae25e40e2631c3ef" 
    }, 
    "name": "Bob Smith", 
    "email": "bob@smith.com", 
    "health": { 
        "height": 192, 
        "weight": 85 
    }
}]

fmt.Println 解码后的contacts 数组的输出:

[{{} Bob Smith bob@smith.com {192 85}}]

【问题讨论】:

    标签: mongodb go


    【解决方案1】:

    感谢this excellent tutorialthis anwser 我能够找到答案。

    我需要将结构中的ID 设置为primitive.ObjectID,并确保我已导入"go.mongodb.org/mongo-driver/bson/primitive"

    type Contact struct {
        ID      primitive.ObjectID  `json:"_id" bson:"_id"
        Name    string `json:"name" bson:"name"`
        Email   string `json:"email" bson:"email"`
        Health  struct {
            Weight  int `json:"weight" bson:"weight"`
            Height  int `json:"height" bson:"height"`
        } `json:"health" bson:"health"`    
    }
    

    对于那些希望使用官方 MongoDB Go 驱动程序的人,请参阅下面的本教程,该教程提供了很好的解释和示例,说明如何执行基本 REST api 等所需的所有 CRUD 操作。

    Using the official MongoDB Go driver

    【讨论】:

    • 虽然我通过您的回答找到了解决方案,但实际上对我有用的是添加bson 标签,无论ID 字段类型如何(我使用string,但我也尝试过primitive.ObjectID 并且在我尝试使用 _id bson 结构标记对其进行解码之前,两者都不起作用
    【解决方案2】:

    在我看来,您缺少“bson”标签。您已正确标记 json,但未正确标记 bson。其余字段都很好,因为它们默认为正确的 bson 标签,但“_id”默认为“id”。尝试添加

    json:"_id" bson:"_id"

    【讨论】:

    • 感谢您的建议,但这无济于事。奇怪的是,当我将json:"_id" 更改为json:"_id" bson:"_id" 整个结构变为空白。 :-/
    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多