After using InsertOne
to create a new document, when I return the result I'm getting an array of numbers rather than an ObjectID
. In the DB, the id is generating fine.
type User struct {
ID string
Email string
Username string
Password string
}
var db = ...
// UserStore creates user
func UserStore(c echo.Context) (err error) {
coll := db.Collection("users")
u := new(User)
if err = c.Bind(u); err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
result, err := coll.InsertOne(
context.Background(),
bson.NewDocument(
bson.EC.String("email", u.Email),
bson.EC.String("username", u.Username),
bson.EC.String("password", u.Password),
),
)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusCreated, result)
}
This returns something like InsertedID: [90, 217, 85, 109, 184, 249, 162, 204, 249, 103, 214, 121]
instead of a normal ObjectID
. How can I return the actual ObjectID
from the newly inserted document?