dongxizhe9755
2017-01-23 01:17
浏览 40

使用mgo在MongoDB中插入数据

I'm trying to insert some data in MongoDB using mgo but the outcome is not what I wanted.

My struct

    type Slow struct {
    Endpoint string
    Time     string
    }

My insert statement

err := collection.Insert(&Slow{endpoint, e}) 
if err != nil {
    panic(err)
}

How I'm trying to print it

    var results []Slow

    err := collection.Find(nil).All(&results)
    if err != nil {
        panic(err)
    }   
    s, _ := json.MarshalIndent(results, "  ", "  ")
    w.Write(s)

My output (Marshaled JSON)

   [{
       "Endpoint": "/api/endpoint1",
       "Time": "0.8s"
    },
    {
       "Endpoint": "/api/endpoint2",
       "Time": "0.7s"
    }]

What I wanted

    {
      "/api/endpoint1":"0.8s",
      "/api/endpoint2":"0.7s"
    }
    //No brackets

Thank you.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题