使用InsertOne
创建新文档后,当我返回结果时,我得到一个数字数组而不是ObjectID
. 在数据库中,id 生成良好。
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)
}
这会返回类似InsertedID: [90, 217, 85, 109, 184, 249, 162, 204, 249, 103, 214, 121]
而不是正常的ObjectID
. 如何ObjectID
从新插入的文档中返回实际值?