mongodb - Retrieving document from collection by id -
my object have in collection:
type room struct { id bson.objectid `json:"id" bson:"_id"` name string `json:"name" bson:"name"` }
inserting collection:
room = &room{id: bson.newobjectid(), name: "test"} roomcollection.insert(room)
retrieving collection (any):
roomx := &room{} if err := roomcollection.find(bson.m{}).one(roomx); err != nil { panic(err) } fmt.printf("roomx %s:\n%+v\n\n", roomx.id, roomx)
this outputs:
roomx objectidhex("52024f457a7ea6334d000001"): &{id:objectidhex("52024f457a7ea6334d000001") name:test}
retrieving collection (by id):
roomz := &room{} if err := roomcollection.find(bson.m{"_id": room.id}).one(roomz); err != nil { panic(err) // throws "not found" }
this throws "not found" , can't figure out why.
the different key-value tags field should, according reflect
package, separated space.
by convention, tag strings concatenation of optionally space-separated key:"value" pairs. each key non-empty string consisting of non-control characters other space (u+0020 ' '), quote (u+0022 '"'), , colon (u+003a ':'). each value quoted using u+0022 '"' characters , go string literal syntax.
the mgo
package fails read tag , stores id value id
instead of _id
.
Comments
Post a Comment