mgo

finding by _id returns an empty object if no tags are defined

Bug #1284380 reported by Robert Xu
6
This bug affects 1 person
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:"_id,omitempty"`
    Name string `bson:"Name"`
}

type NonWorking struct {
    Id bson.ObjectId `bson:"_id,omitempty"`
    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.

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

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
    http://labix.org/v2/mgo/bson#Unmarshal

Changed in mgo:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.