需要帮助使用Golang将类型为interface的地图存储到我的MongoDB数据库中。

huangapple go评论83阅读模式
英文:

Need help to storing an map of type interface in my mongodb database using golang

问题

我正在创建一个应用程序,其中后端使用Go语言,数据库使用MongoDB。我的问题是,我在结构体中声明了一个map,像这样:

Data struct {
    data   map[interface{}]interface{}
}

在添加值之后,像这样:

var data Data
data["us"]="country"
data[2]="number"
data["mother"]="son"

我像这样插入它:

c.Insert(&data)

当我插入后,我丢失了键,只能看到值...

{
    "_id" : Object Id("57e8d9048c1c6f751ccfaf50"),
    "data" : {
        "<interface {} Value>" : "country",
        "<interface {} Value>" : "number",
        "<interface {} Value>" : "son"
    },
   
}

请问有没有办法在MongoDB中使用接口来获取键和值?谢谢...

英文:

I m in the process of creating application where my back end is in go lang and database is mongoDB. My problem is that i have a map in my struct declared like

Data struct {
    data   map[interface{}]interface{}
}

after adding values in to this like

var data Data
    data[&quot;us&quot;]=&quot;country&quot;
    data[2]=&quot;number&quot;
    data[&quot;mother&quot;]=&quot;son&quot;

I m inserting it like

c.Insert(&amp;data)

When i insert this i m losing my key and can only see the values...

{
    &quot;_id&quot; : Object Id(&quot;57e8d9048c1c6f751ccfaf50&quot;),
    &quot;data&quot; : {
        &quot;&lt;interface {} Value&gt;&quot; : &quot;country&quot;,
        &quot;&lt;interface {} Value&gt;&quot; : &quot;number&quot;,
        &quot;&lt;interface {} Value&gt;&quot; : &quot;son&quot;
    },
   
}

May i know any way possible to use interface and get both key and values in my mongoDB. Thanks....

答案1

得分: 3

在MongoDB文档中,你只能使用string作为键。即使你将Data结构定义为map[int]interface{},MongoDB(不知道mgo是否会转换类型)也不允许你将该对象插入数据库。实际上,你在JSON中只能使用string作为键,因为这不是有效的JSON(在浏览器控制台中尝试运行以下代码JSON.parse('{2:"number"}'))。

因此,将你的Data定义为bson.Mmap[string]interface{}的快捷方式),并使用strconv包将数字转换为字符串。

但我猜你可能需要考虑数组/切片,因为将数字作为JSON键的唯一原因是以后需要通过这些字段进行迭代。而对于迭代,我们使用数组。

更新:刚刚检查了mgo如何处理map[int]interface{}。它将其插入数据库条目中,格式为{"<int Value>": "hello"}。其中<int Value>不是数字,而是实际的string <int Value>

英文:

You can use nothing but string as key in MongoDB documents. Even if you would define your Data structure as map[int]interface{} Mongo (don't know if mgo will convert types) wouldn't allow you to insert this object into the database. Actually, you can use nothing but string as JSON key at all as this wouldn't be JSON (try in your browser console the next code JSON.parse(&#39;{2:&quot;number&quot;}&#39;)).

So define your Data as bson.M (shortcut for map[string]interface{}) and use strconv package to convert your numbers into strings.

But I guess you must look at arrays/slices, as only one reason why someone may need to have numbers as keys in JSON is iterations through these fields in future. And for iterations we use arrays.

Update: just checked how mgo deals with map[int]interface{}. It inserts into DB entry like {&quot;&lt;int Value&gt;&quot; : &quot;hello&quot;}. Where &lt;int Value&gt; is not number but actually string &lt;int Value&gt;

huangapple
  • 本文由 发表于 2016年9月26日 16:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/39697851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定