英文:
How to get the data from mongodb with all the struct variables?
问题
在这段代码中,我试图从MongoDB数据库中访问Cardname
,但它给我返回一个空字符串。尽管在Cardname
之前打印了所有变量,但在Cardname
和Cardname
之后的变量都没有打印出来。
type UserCredentials struct {
Fname string
Lname string
Email string
Password string
Phone string
Country string
State string
Faddress string
Laddress string
Postal string
Company string
Cardname string
Cardnumber string
Expmonth string
Expyear string
}
func FindCard(email, password string) {
var uc UserCredentials
collection := Connect.Database("eCommerce").Collection("register")
if err := collection.FindOne(context.TODO(), bson.M{"email": email, "password": password}).Decode(&uc); err != nil {
log.Fatal(err)
}
fmt.Println(uc.Cardname)
}
在这段代码中,UserCredentials
是一个结构体,它包含了多个字段,其中包括Cardname
。FindCard
函数用于从数据库中查找具有指定邮箱和密码的用户,并将结果存储在uc
变量中。然后,它打印出uc.Cardname
的值。
请注意,这段代码中使用了一些其他的自定义函数和变量,比如Connect.Database
和bson.M
,你可能需要在其他地方定义它们。
英文:
In this code, I am trying to access the Cardname
from the MongoDB database but it is giving me an empty string. Although it prints all the variables before Cardname
but does not print the variables after Cardname
and Cardname
itself.
type UserCredentials struct {
Fname string
Lname string
Email string
Password string
Phone string
Country string
State string
Faddress string
Laddress string
Postal string
Company string
Cardname string
Cardnumber string
Expmonth string
Expyear string
}
func FindCard(email, password string) {
var uc UserCredentials
collection := Connect.Database("eCommerce").Collection("register")
if err := collection.FindOne(context.TODO(), bson.M{"email": email, "password": password}).Decode(&uc); err != nil {
log.Fatal(err)
}
fmt.Println(uc.Cardname)
}
答案1
得分: 2
mongo驱动程序无法自动找出要将哪个文档字段设置到哪个结构字段中。
有一些“常识”规则,例如字段名称与属性匹配(即使首字母没有大写),但字段名Cardname
不会与属性名“card name”
匹配。
您必须使用结构标签告诉映射,即bson
结构标签(这是mongo-go驱动程序使用的)。
例如:
type UserCredentials struct {
Fname string
Lname string
Email string
Password string
Phone string
Country string
State string
Faddress string
Laddress string
Postal string
Company string
Cardname string `bson:"card name"`
Cardnumber string `bson:"card number"`
Expmonth string `bson:"expiry month"`
Expyear string `bson:"expiry year"`
}
英文:
The mongo driver will not magically find out which document field you want to set into which struct field.
There are some "common sense" rules, such as field names matching properties (even if the first letter is not capitalized), but the field name Cardname
will not be matched with the property name "card name"
.
You have to tell the mapping using struct tags, namely the bson
struct tag (this is what the mongo-go driver uses).
For example:
type UserCredentials struct {
Fname string
Lname string
Email string
Password string
Phone string
Country string
State string
Faddress string
Laddress string
Postal string
Company string
Cardname string `bson:"card name"`
Cardnumber string `bson:"card number"`
Expmonth string `bson:"expiry month"`
Expyear string `bson:"expiry year"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论