插入 ISODate 字段使用 mgo 出现错误。

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

Inserting ISODate field using mgo error

问题

我真的很新使用go、mgo和gin gonic...我正在创建一个小应用程序,但在使用mgo将新记录插入到mongoDB时遇到了问题。我的错误信息如下:

> "PANIC: 解析字段documents的第0个元素时出错 :: caused by :: '0'字段的类型错误,期望是对象,但找到的是0: [ { date: new Date(1441051152939), from: "11", to: "12", office: "2", client_id: "1368465545" } ]_"

我的结构体如下:

type Reservation struct {
      ID        bson.ObjectId `bson:"_id,omitempty" json:"_id"`
      Date      time.Time     `bson:"date" json:"date"`
      From      string        `bson:"from" json:"from"`
      To        string        `json:"to"`
      Office     string       `json:"office"`
      Client_id string        `json:"client_id"}`

我尝试使用gin-gonic和mgo进行插入操作,代码如下:

func addReservation(c *gin.Context) {
    
      x := session.DB("projXXXX").C("reservation")
      var reservations []Reservation
      c.Bind(&reservations)
      err := x.Insert(&reservations)
      if err != nil {
            panic(err)
      }
      c.String(200,"whatever")  
}

我的mongoDB集合如下:

{
    "_id" : ObjectId("55ba2e611cb87b9a6d75e94b"),
    "date" : ISODate("2015-10-22T00:00:00.000Z"),
    "from" : "9",
    "to" : "10",
    "office" : "4",
    "client_id" : "1123456469797"
}

非常感谢你的帮助。

英文:

I'm really new in using go, mgo and gin gonic ...I've been creating a mini app and I have a problem inserting a new register into mongoDB using mgo. My error says:

> "PANIC: error parsing element 0 of field documents :: caused by ::
> wrong type for '0' field, expected object, found 0: [ { date: new
> Date(1441051152939), from: "11", to: "12", office: "2", client_id:
> "1368465545" } ]_"

My struct is the next one:

type Reservation struct {
      ID        bson.ObjectId `bson:"_id,omitempty" json:"_id"`
      Date      time.Time     `bson:"date" json:"date"`
      From      string        `bson:"from" json:"from"`
      To        string        `json:"to"`
      Office     string       `json:"office"`
      Client_id string        `json:"client_id"` }

And I'm trying to insert it as follows using gin-gonic and mgo:

    func addReservation(c *gin.Context) {
    
          x := session.DB("projXXXX").C("reservation")
          var reservations []Reservation
          c.Bind(&reservations)>             
          err := x.Insert(&reservations)
          if err != nil {
                panic(err)
          }
          c.String(200,"whatever")  
}

My collection in mongoDB is like this:

{
    "_id" : ObjectId("55ba2e611cb87b9a6d75e94b"),
    "date" : ISODate("2015-10-22T00:00:00.000Z"),
    "from" : "9",
    "to" : "10",
    "office" : "4",
    "client_id" : "1123456469797"
}

Thanks a lot for your help

答案1

得分: 4

从错误的表现来看,MongoDB在期望看到一个单个对象的位置上看到了一个数组。看起来问题是你试图将[]Reservation切片作为单个对象插入。

而不是将对象切片作为插入的参数,Collection.Insert函数将每个要插入的对象作为单独的参数传入。你可能想要使用特殊的...语法来调用可变参数函数

err := x.Insert(reservations...)
英文:

From the look of the error, MongoDB is seeing an array where it expects to see a single object. It looks like the problem is that you're trying to insert the []Reservation slice as a single object.

Rather than taking a slice of objects to insert, Collection.Insert takes each object to insert as a separate argument. You probably want to use the special ... syntax for calling a variadic function:

err := x.Insert(reservations...)

huangapple
  • 本文由 发表于 2015年9月1日 04:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/32319424.html
匿名

发表评论

匿名网友

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

确定