如何从MongoDB中获取具有所有结构变量的数据?

huangapple go评论83阅读模式
英文:

How to get the data from mongodb with all the struct variables?

问题

在这段代码中,我试图从MongoDB数据库中访问Cardname,但它给我返回一个空字符串。尽管在Cardname之前打印了所有变量,但在CardnameCardname之后的变量都没有打印出来。

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是一个结构体,它包含了多个字段,其中包括CardnameFindCard函数用于从数据库中查找具有指定邮箱和密码的用户,并将结果存储在uc变量中。然后,它打印出uc.Cardname的值。

请注意,这段代码中使用了一些其他的自定义函数和变量,比如Connect.Databasebson.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.
如何从MongoDB中获取具有所有结构变量的数据?

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"`
}

huangapple
  • 本文由 发表于 2022年2月8日 17:41:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/71031622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定