写个golang可以解析map[_id:map[_data:8263B4EB63000000062B022C0100296E5A1004C3E4524B77B64630AC204C7469FAED7F46645F6964006463B4EB636EBBDB249F2ED2770004] clusterTime:{1672801123 6} documentKey:map[_id:ObjectID("63b4eb636ebbdb249f2ed277")] fullDocument:map[OwnerID:123 QuotaSize:104857600 _id:ObjectID("63b4eb636ebbdb249f2ed277") directory:/buckets name:123_test1] ns:map[coll:UserQuotaConfig db:filer3] operationType:insert wallTime:1672801123879]中fullDocument的OwnerID、name、QuotaSize的方法
时间: 2023-02-09 10:57:10 浏览: 228
neodigm:Neodigm WASM GoLang博客:light_bulb:
你可以这样解析出 map 中的 fullDocument 中的 OwnerID、name、QuotaSize:
```
package main
import (
"fmt"
"reflect"
)
func main() {
m := map[string]interface{}{
"_id": map[string]interface{}{
"_data": "8263B4EB63000000062B022C0100296E5A1004C3E4524B77B64630AC204C7469FAED7F46645F6964006463B4EB636EBBDB249F2ED2770004",
},
"clusterTime": map[string]interface{}{
"1672801123": 6,
},
"documentKey": map[string]interface{}{
"_id": "ObjectID(\"63b4eb636ebbdb249f2ed277\")",
},
"fullDocument": map[string]interface{}{
"OwnerID": 123,
"QuotaSize": 104857600,
"_id": "ObjectID(\"63b4eb636ebbdb249f2ed277\")",
"directory": "/buckets",
"name": "123_test1",
},
"ns": map[string]interface{}{
"coll": "UserQuotaConfig",
"db": "filer3",
},
"operationType": "insert",
"wallTime": 1672801123879,
}
// 取出 fullDocument
fullDocument, ok := m["fullDocument"].(map[string]interface{})
if !ok {
fmt.Println("fullDocument not found")
return
}
// 取出 OwnerID
OwnerID, ok := fullDocument["OwnerID"].(int)
if !ok {
fmt.Println("OwnerID not found")
return
}
fmt.Println("OwnerID:", OwnerID)
// 取出 name
name, ok := fullDocument["name"].(string)
if !ok {
fmt.Println("name not found")
return
}
fmt.Println("name:", name)
// 取出 QuotaSize
QuotaSize, ok := fullDocument["QuotaSize"].(int)
if !ok {
fmt.Println("QuotaSize not found")
return
}
fmt.Println("QuotaSize:", QuotaSize)
}
```
在这个例子中,我们首先将 map 赋值给了变量 m。然后使用类型断言取出了 fullDocument 并将其赋
阅读全文