伙計(jì)們,我正在嘗試使用用戶電話查詢 MongoDB 和 golan 中的集合,獲取用戶的 ID 并使用它來(lái)查詢另一個(gè)集合,但是當(dāng)我嘗試使用該返回 ID 時(shí),它給我一個(gè)錯(cuò)誤cannot use userid (variable of type interface{}) as string value in argument to primitive.ObjectIDFromHex: need type assertion我的代碼var result bson.M err := userdataCollection.FindOne(context.TODO(), bson.M{"phone":"+2347000000"}).Decode(&result) if err != nil { if err == mongo.ErrNoDocuments { // This error means your query did not match any documents. return } log.Fatal(err) } var userid = result["_id"] fmt.Printf("var6 = %T\n", userid) json.NewEncoder(w).Encode(result["_id"]) // this returns prints the user id id,_ := primitive.ObjectIDFromHex(userid) // where I am having the error
使用 golan 查詢 mongodb 中的集合并將 id 作為字符串返回
慕蓋茨4494581
2023-03-07 11:45:18