3
type (

    Id struct {
        // I previously forgot to add the `ID` field in my question, it is present in my code but not on this question as @icza pointed it out to me
        ID bson.ObjectId `json:"id" bson:"_id"`
    }

    User struct {
        // When using anonymous struct, upon returning the collection, each of the document will have an empty `id` field, `id: ""`
        Id
        Email string `json:"email" bson:"email"`
        ...
    }

    // This works
    User2 struct {
        ID bson.ObjectId `json:"id" bson:"_id"`
        Email string `json:"email" bson:"email"`
    }
)

I might not have fully understood the concept of anonymous structs yet. In the example above, when querying all users from a collection, the id field is going to be an empty string "". However, if I directly define the ID field in the User struct, the id shows up fine. Is this not what anonymous structs are for? Basically extending struct so you won't have to type them again and again?

More example:

type SoftDelete struct {
    CreatedAt time.Time `json:"created_at" bson:"created_at"`
    UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
    DeletedAt time.Time `json:"deleted_at" bson:"deleted_at"`
}

type UserModel struct {
    SoftDelete
}

type BlogPost struct {
    SoftDelete
}

1 Answer 1

5

The problem here is that fields having struct types (including embedded structs) appear as embedded documents in MongoDB. If you don't want this, you have to specify the "inline" bson flag when embedding a struct:

User struct {
    Id           `bson:",inline"`
    Email string `json:"email" bson:"email"`
}

What the "inline" tag does is in MongoDB it "flattens" the fields of the embedded struct as if they were part of the embedder struct.

Similarly:

type UserModel struct {
    SoftDelete `bson:",inline"`
}

type BlogPost struct {
    SoftDelete `bson:",inline"`
}

Edit: the following section applies to the original Id type which embedded bson.ObjectId. The asker later clarified that this was just a typo (and edited the question since), and it is a named, non-anonymous field. Still think the info below is useful.

One thing to note about your Id type: Your Id type also embeds bson.ObjectId:

Id struct {
    bson.ObjectId `json:"id" bson:"_id"`
}

Id not just has a field of bson.ObjectId, but it embeds it. This matters because this way you Id type will have a String() method promoted from bson.ObjectId, and so will User which embeds Id. Having said that, it will be hard trying to print or debug values of type User, because you will always see it printed just as a single ObjectId.

Instead you can make it a regular field in Id, embedding Id in User will still work as expected:

Id struct {
    ID bson.ObjectId `json:"id" bson:"_id"`
}

See related question+asnwer: Enforce a type mapping with mgo

Sign up to request clarification or add additional context in comments.

1 Comment

Ohh sorry, I forgot to type ID in the first code block. It's actually there in my code I just forgot to add it to this question. Thanks for pointing out that inline tag. That did it for me, thanks heaps! Btw, you have now answered 2 of my mgo questions with both as accepted! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.