finding by _id returns an empty object if no tags are defined
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
mgo |
Invalid
|
Undecided
|
Unassigned |
Bug Description
The bug occurs when trying to Find(bson.M{"_id", <id>}) (or use the relevant FindId() method) with a struct that does not explicitly define `bson:"BSON_NAME"`. Instead of getting the object, it simply returns your requested struct, with the only field filled being the ID. It should do reflection and retrieve struct member names like it does with Insert(), but it doesn't.
Typically, if there is no explicit struct tag definition for bson on a struct member, it should use the member name. If I find by anything other than "_id", it does this and acts as expected. So it only seems to be occurring whenever I filter with "_id".
Testcase: on the go1 tag (which should be latest, last I checked), if I have the following structs:
type Working struct {
Id bson.ObjectId `bson:"
Name string `bson:"Name"`
}
type NonWorking struct {
Id bson.ObjectId `bson:"
Name string
}
Calling FindId() with the Working struct will act as expected.
Calling FindId() with the NonWorking struct will return said struct with only its Id, and its Name initialised to its empty value.
The default key is taken from the lowercased field name, because that's the most common convention in the MongoDB world. So it's not that it doesn't work, but rather working with a field name that you do not expect.
These rules are all explained here:
http:// labix.org/ v2/mgo/ bson#Marshal labix.org/ v2/mgo/ bson#Unmarshal
http://