0

So recently I've been trying to insert different types of data into structures using mgo, and I've ran into a bit of a snag. When trying to insert an array of object IDs, I can't seem to figure out that format if I were to populate that object ID array within the structure.

Here's the structure as follows

type Group struct {
ID        bson.ObjectId     `json:"id" bson:"_id"`
GroupName string            `json:"groupName"`
UserIDs   []*bson.ObjectId      `json:"users" bson:"userid"`
Expected  []*int            `json:"expected"`
Actual    []*int            `json:"actual"`
}

And the operation I am trying to run is to insert a new "test" table with a single userID into UserIDs.

array := []*bson.ObjectId{&findJ.ID}

c = Session.DB("test").C("Group")
err = c.Insert(&Group{GroupName: "test", UserIDs: array})
ThisPanic(err) 

Where findJ has it's own ID from a separate structure. What am I doing wrong here, as it is causing a panic on inserting the array.

Phlex
  • 401
  • 2
  • 6
  • 17
  • Thanks for the tip about the pointers! I will get the actual code output here shortly. I tried playing around with it before, but was still getting errors (on the array I had a different declaration), but I will get the output here soon! – Phlex Nov 24 '15 at 20:47
  • Couldn't edit my comment, but here's the output of the error: `Cannot take the address of bson.objectId(findJ.ID)` – Phlex Nov 24 '15 at 20:53
  • Also, another error that has been popping up is `ObjectIDs must be exactly 12 bytes long (got 0)`. – Phlex Nov 24 '15 at 21:05
  • After removing all pointers, the aforementioned of a 0 byte ObjectID was passed. Thanks for your help so far! – Phlex Nov 24 '15 at 21:30
  • Sorry, I mean the error specifically is saying it's not passing the proper ID. As in, if I print findJ.ID before, it outputs the value `ObjectIdHex("5654df213610fec33e7eb342") panic: ObjectIDs must be exactly 12 bytes long (got 0) ` and then the panic happens right after, when it is trying to insert into the group (and yes, I am switching collections to group itself too) – Phlex Nov 24 '15 at 22:07

0 Answers0