How to find by id in golang and mongodb

I need to get values โ€‹โ€‹using ObjectIdHex and update as well as view the result. I use mongodb and golang. But the following code is not working properly

package main import ( "fmt" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type Person struct { Id bson.ObjectId `json:"id" bson:"_id,omitempty"` Name string Phone string } func checkError(err error) { if err != nil { panic(err) } } const ( DB_NAME = "gotest" DB_COLLECTION = "pepole_new1" ) func main() { session, err := mgo.Dial("localhost") checkError(err) defer session.Close() session.SetMode(mgo.Monotonic, true) c := session.DB(DB_NAME).C(DB_COLLECTION) err = c.DropCollection() checkError(err) ale := Person{Name:"Ale", Phone:"555-5555"} cla := Person{Name:"Cla", Phone:"555-1234-2222"} kasaun := Person{Name:"kasaun", Phone:"533-12554-2222"} chamila := Person{Name:"chamila", Phone:"533-545-6784"} fmt.Println("Inserting") err = c.Insert(&ale, &cla, &kasaun, &chamila) checkError(err) fmt.Println("findbyID") var resultsID []Person //err = c.FindId(bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")).One(&resultsID) err = c.FindId(bson.M{"Id": bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")}).One(&resultsID) checkError(err) if err != nil { panic(err) } fmt.Println("Phone:", resultsID) fmt.Println("Queryingall") var results []Person err = c.Find(nil).All(&results) if err != nil { panic(err) } fmt.Println("Results All: ", results) } 

FindId(bson.M{"Id": bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")}).One(&resultsID) did not work for me and gave me the following result

 Inserting Queryingall Results All: [{ObjectIdHex("56bddee2cfa93bfe3d3504a1") Ale 555-5555} {ObjectIdHex("56bddee2cfa93bfe3d3504a2") Cla 555-1234-2222} {ObjectIdHex("56bddee2cfa93bfe3d3504a3") kasaun 533-12554-2222} {ObjectIdHex("56bddee2cfa93bfe3d3504a4") chamila 533-545-6784}] findbyID panic: not found goroutine 1 [running]: main.checkError(0x7f33d524b000, 0xc8200689b0) 56bddee2cfa93bfe3d3504a1") Ale 555-5555} {ObjectIdHex ( "56bddee2cfa93bfe3d3504a2") Cla 555-1234-2222} {ObjectIdHex ( "56bddee2cfa93bfe3d3504a3") kasaun 533-12554-2222} {ObjectIdHex ( "56bddee2cfa93bfe3d3504a4" Inserting Queryingall Results All: [{ObjectIdHex("56bddee2cfa93bfe3d3504a1") Ale 555-5555} {ObjectIdHex("56bddee2cfa93bfe3d3504a2") Cla 555-1234-2222} {ObjectIdHex("56bddee2cfa93bfe3d3504a3") kasaun 533-12554-2222} {ObjectIdHex("56bddee2cfa93bfe3d3504a4") chamila 533-545-6784}] findbyID panic: not found goroutine 1 [running]: main.checkError(0x7f33d524b000, 0xc8200689b0) 

How can I fix this problem? I need to get the value using oid and update as well how can I do this

+10
mongodb go crud
source share
2 answers

This should be _id not Id :

 c.FindId(bson.M{"_id": bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")}) 
+12
source share

Usage can do the same with the official Golang driver as follows

 userID,err := primitive.ObjectIDFromHex("5b9223c86486b341ea76910c") if err != nil{ log.Println("Invalid ObjectID") } result:= client.Database(database).Collection("user").FindOne(context.Background(), bson.M{"_id": userID}) user := model.User{} result.Decode(user) 
0
source share

All Articles