InsertOne returns interface{}

Hi Go Community,

I just wanna say to begin with I’m pretty new to go but getting into the mongo world have been pretty painless, I just have a question in regards to the retun value of InsertOne:

Why does it return interface{} rather than primitive.ObjectID ?

I just find it a bit akward to do like so:

id, err := primitive.ObjectIDFromHex(fmt.Sprintf("%s", result.InsertedID))

for creating aobject Id for my Go native struct’s to be happy, why not just return it as ObjectID?

Hello @Lars_Krieger, welcome to the MongoDB Community forum!

result.InsertedID returns the _id value as ObjectId. You don’t need the code as: primitive.ObjectIDFromHex(...). If you want the _id value in a variable you can do this and it will the ObjectId:

newId := result.InsertedID

[ EDIT ADD ]

fmt.Printf("Inserted document _id type: %v\n", reflect.TypeOf(newId))

This will print primitive.ObjectID.

Well according to this: mongo package - go.mongodb.org/mongo-driver/mongo - pkg.go.dev

it returns as interface, and thus would not place nice with my struct where it is defined like so:

type Entity struct {
  Id primitive.ObjectID
}

And go would throw the following error:

cannot use result.InsertedID (variable of type interface{}) as primitive.ObjectID value in struct literal

You can use this:

id := newId.(primitive.ObjectID)
myst1 := MyStruct{ id, "title1", "author1" }

Assuming:

type MyStruct struct {
	Id primitive.ObjectID
	Title string
	Author string
}

I believe the error is about Type Assertions in Golang.

@Lars_Krieger to answer your specific question:

MongoDB supports many different types in the _id field (e.g. ObjectID, string, int, etc; see Field Names for more information), so the Go driver has to return the _id as an interface{} because it won’t always be an ObjectID.

For example, if you use a string for _id, you get back a string:

res, _ := coll.InsertOne(context.Background(), bson.D{{"_id", "abcd"}, {"key", "value"}})
fmt.Printf("%v, type = %T\n", res.InsertedID, res.InsertedID)
// abcd, type = string

If you use an int for _id, you get back an int32:

res, _ := coll.InsertOne(context.Background(), bson.D{{"_id", 1234}, {"key", "value"}})
fmt.Printf("%v, type = %T\n", res.InsertedID, res.InsertedID)
// 1234, type = int32

If you don’t specify an _id, the driver automatically generates an ObjectID:

res, _ := coll.InsertOne(context.Background(), bson.D{{"key", "value"}})
fmt.Printf("%v, type = %T\n", res.InsertedID, res.InsertedID)
// ObjectID(61895f6da6881b5cfc508e65), type = primitive.ObjectID

Ah okay thanks @Matt_Dale, I’m also fairly new to mongo db, then it makes sense it returns as interface, i thought it would always be a ObjectID.

But just to be sure the _id property is the default ID key for the mongo database. A user can overwrite what this value will be doing creation of the document, so it could be a string, int or a ObjectID, but if its not specified by the developer the mongo engine would default to ObjectID?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.