匿名结构 return 空字段值
Anonymous structs return empty field value
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"`
}
)
我可能还没有完全理解匿名结构的概念。在上面的示例中,当查询集合中的所有用户时,id
字段将是一个空字符串 ""
。但是,如果我直接在 User
结构中定义 ID 字段,id
会正常显示。这不是匿名结构的用途吗?基本上是扩展结构,这样您就不必一次又一次地输入它们了?
更多示例:
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
}
这里的问题是具有 struct
类型(包括嵌入式结构)的字段在 MongoDB 中显示为嵌入式文档。如果你不想这样,你必须在嵌入结构时指定 "inline"
bson 标志:
User struct {
Id `bson:",inline"`
Email string `json:"email" bson:"email"`
}
"inline"
标签所做的是 MongoDB 它 "flattens" 嵌入结构的字段,就好像它们是嵌入结构的一部分一样。
同样:
type UserModel struct {
SoftDelete `bson:",inline"`
}
type BlogPost struct {
SoftDelete `bson:",inline"`
}
编辑: 以下部分适用于嵌入 bson.ObjectId
的原始 Id
类型。提问者后来澄清说这只是一个拼写错误(并从那以后编辑了问题),它是一个命名的 non-anonymous 字段。还是觉得下面的信息有用。
关于您的 Id
类型需要注意的一件事:您的 Id
类型还 embeds bson.ObjectId
:
Id struct {
bson.ObjectId `json:"id" bson:"_id"`
}
Id
不仅具有 bson.ObjectId
的字段,而且还嵌入了它。这很重要,因为这样你 Id
类型将有一个从 bson.ObjectId
提升的 String()
方法,嵌入 Id
的 User
也是如此。话虽如此,尝试打印或调试 User
类型的值将很困难,因为您总是会看到它被打印为单个 ObjectId
.
相反,您可以将其设为 Id
中的常规字段,将 Id
嵌入 User
仍将按预期工作:
Id struct {
ID bson.ObjectId `json:"id" bson:"_id"`
}
查看相关问题+答案:
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"`
}
)
我可能还没有完全理解匿名结构的概念。在上面的示例中,当查询集合中的所有用户时,id
字段将是一个空字符串 ""
。但是,如果我直接在 User
结构中定义 ID 字段,id
会正常显示。这不是匿名结构的用途吗?基本上是扩展结构,这样您就不必一次又一次地输入它们了?
更多示例:
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
}
这里的问题是具有 struct
类型(包括嵌入式结构)的字段在 MongoDB 中显示为嵌入式文档。如果你不想这样,你必须在嵌入结构时指定 "inline"
bson 标志:
User struct {
Id `bson:",inline"`
Email string `json:"email" bson:"email"`
}
"inline"
标签所做的是 MongoDB 它 "flattens" 嵌入结构的字段,就好像它们是嵌入结构的一部分一样。
同样:
type UserModel struct {
SoftDelete `bson:",inline"`
}
type BlogPost struct {
SoftDelete `bson:",inline"`
}
编辑: 以下部分适用于嵌入 bson.ObjectId
的原始 Id
类型。提问者后来澄清说这只是一个拼写错误(并从那以后编辑了问题),它是一个命名的 non-anonymous 字段。还是觉得下面的信息有用。
关于您的 Id
类型需要注意的一件事:您的 Id
类型还 embeds bson.ObjectId
:
Id struct {
bson.ObjectId `json:"id" bson:"_id"`
}
Id
不仅具有 bson.ObjectId
的字段,而且还嵌入了它。这很重要,因为这样你 Id
类型将有一个从 bson.ObjectId
提升的 String()
方法,嵌入 Id
的 User
也是如此。话虽如此,尝试打印或调试 User
类型的值将很困难,因为您总是会看到它被打印为单个 ObjectId
.
相反,您可以将其设为 Id
中的常规字段,将 Id
嵌入 User
仍将按预期工作:
Id struct {
ID bson.ObjectId `json:"id" bson:"_id"`
}
查看相关问题+答案: