无法在Golang中从MongoDB结果解码ObjectId子值 [英] Unable to Decode the ObjectId SubValue from MongoDB results in Golang
问题描述
我正在使用 MongoDb Go驱动程序,但无法获取ObjectId在我的结构中解码的JSON的子值.
I am using the MongoDb Go Driver and I am unable to get the ObjectId subvalue from the JSON decoded in my structs.
Note: I am using a different library/API than this question, so please don't mark this as duplicate.
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"
)
我有一种类似的结构用于处理结果
I have a type of struct like this for processing the results
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"`
}
然后我像这样检索联系人:
And then I go to retrieve the contacts like this:
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"
部分的子字段.有人可以帮我吗? ?
The subfields for "health"
appear exactly as they should, but for some reason the subfield from the "_id"
part of the results is nowhere to be found. Can anyone help me with this??
从数据库发出的JSON响应是这样的,由于某种原因,我能够获取health
字段的子字段,但不能获取_id
字段的子字段.为什么不?
The JSON response from the database comes out like this, and for some reason I am able to get the subfields for the health
field, but not the _id
field. Why not?
数据库的原始JSON响应
Raw JSON response of DB
[{
"_id": {
"$obj": "5c601648ae25e40e2631c3ef"
},
"name": "Bob Smith",
"email": "bob@smith.com",
"health": {
"height": 192,
"weight": 85
}
}]
fmt.Println
的解码后的contacts
数组的输出:
fmt.Println
's output of the decoded contacts
array:
[{{} Bob Smith bob@smith.com {192 85}}]
推荐答案
Thanks to this excellent tutorial and this anwser I was able to find the answer.
我需要在结构中将ID
设置为primitive.ObjectID
,并确保已导入"go.mongodb.org/mongo-driver/bson/primitive"
I needed to set the ID
in my struct as a primitive.ObjectID
, and made sure I had imported "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操作.
For those looking to use the official MongoDB Go driver, see this tutorial below provides very good explanation and examples of how to do all the CRUD operations necessary for a basic REST api etc.
这篇关于无法在Golang中从MongoDB结果解码ObjectId子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!