drza10046 2013-03-16 13:39
浏览 2

mgo ony最后一个JSON条目

I used the code below to store JSON entry in MongoDB, but only the last entry "c2" is stored. What did I do wrong?

package main

import (
    "encoding/json"
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
)

func insertEntry(j *map[string]interface{}, entry string) {
    err := json.Unmarshal([]byte(entry), &j)
    if err != nil {
        panic(err)
    }

}

func main() {
    c1 := `{
    "mw" : 42.0922,
    "ΔfH°gas" : {
      "value" : 372.38,
      "units" : "kJ/mol"
    },
    "S°gas" : {
      "value" : 216.81,
      "units" : "J/mol×K"
    },
    "index" : [
      {"name" : "mw", "value" : 42.0922},
      {"name" : "ΔfH°gas", "value" : 372.38},
      {"name" : "S°gas", "value" : 216.81}
    ]
    }`

    c2 := `{
    "name": "silicon",
    "mw": 32.1173,
    "index": [
      {
    "name": "mw",
    "value": 32.1173
      }
    ]
    }`

    m := make(map[string]interface{})

    insertEntry(&m, c1)
    insertEntry(&m, c2)
    chemical := m["ΔfH°gas"].(map[string]interface{})
    fmt.Println("value: ", chemical["value"].(float64))
    fmt.Println("units: ", chemical["units"].(string))

    session, err := mgo.Dial("localhost")

    if err != nil {
        panic(err)
    }
    defer session.Close()

    // Optional. Switch the session to a monotonic behavior.
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("test").C("chemicals")
    err = c.Insert(&m)
    if err != nil {
        panic(err)
    }

    result := &m
    err = c.Find(bson.M{"name": "silicon"}).One(&m)
    if err != nil {
        panic(err)
    }

    fmt.Println(result)
    fmt.Println("mw:", m["mw"])

    fmt.Println("value: ", chemical["value"].(float64))
    fmt.Println("units: ", chemical["units"].(string))

}
  • 写回答

1条回答 默认 最新

  • dsmlf1207915 2013-03-16 14:29
    关注

    It looks like the problem is the way you're building up your map. Your call to json.Unmarshall is taking all of the "top-level" entries in your json and making them keys in your map. In this case, those would be mw, ΔfH°gas, S°gas, index, name, mw, and index.

    Note that mw and index are repeated across c1 and c2. That means when you call insertEntry(c2), you're overwriting the mw and index entries. If you print out the contents of your map after each call to insertEntry you can observe this behavior.

    There are a few things you could do to avoid this, depending on your needs. You could reformat your json to avoid these conflicts, you could call c.Insert once for each of your c variables (with a fresh map for each one) which would result in different mongodb documents being created for each json chemical definition instead of one big one containing both.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂