英文:
How to write catalog with many categories(taxonomy >>100)?
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是一个从动态类型语言转到golang的新手。
我遇到了一个问题,如何编写具有许多类别/子类别的目录 - 复杂的分类法。例如:
Shoestring > Shoes > Men > shoes > clothes > Home > Categories
我正在使用mongodb作为后端。我不明白在这种情况下如何编写CRUD操作。
如果我按照通常的方式处理所有查询:
func RunFindAllQuery(document interface{}, m bson.M, mongoSession *mgo.Session, conn Conn) (err error) {
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
collection := sessionCopy.DB(conn.Database).C(conn.Collection)
err = collection.Find(m).All(document)
if err != nil {
log.Printf("RunQuery : ERROR : %s\n", err)
}
return err
}
我将需要定义许多类型:shoes != car.
type Shoes struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Description string `bson:"descriprion"`
Size float `bson:"size"`
Color float `bson:"color"`
Type float `bson:"type"`
ShoeHeight float `bson:"shoeheight"`
PlatformHeight float `bson:"platformheight"`
Country float `bson:"country"`
}
type Car struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Model CarModel `bson:"name"`
Description string `bson:"descriprion"`
Color float `bson:"color"`
Height float `bson:"height"`
Fueltype string `bson:"fueltype"`
Country float `bson:"country"`
}
我的代码将会是复制粘贴的:
var carobjFindAll []Car
m := bson.M{"description": "description"}
_ = RunFindAllQuery(&carobjFindAll, m, mongoSession, conn)
for cur := range carobjFindAll {
fmt.Printf("\nId: %s\n", carobjFindAll[cur].Id)
fmt.Printf("\nColor: %s\n", carobjFindAll[cur].Color)
}
var shoesobjFindAll []Shoes
m_shoes := bson.M{"description": "shoes_description"}
_ = RunFindAllQuery(&shoesobjFindAll, m_shoes, mongoSession, conn)
for cur_shoe := range shoesobjFindAll {
fmt.Printf("\nId: %s\n", shoesobjFindAll[cur_shoe].Id)
fmt.Printf("\nColor: %s\n", shoesobjFindAll[cur_shoe].Color)
}
PS:
对不起,我的英语不好。
英文:
I am newbie to golang, who moved from dynamic typed language.
I faced with problem how to write catalog with many categories/subcategory — complex taxonomy. For example:
Shoestring > Shoes > Men > shoes > clothes > Home > Categories
I am using mongodb as backend. I can't understand how to write CRUD operation's in this case?
If I will process all queries as usual:
<!-- language: «lang-go» -->
func RunFindAllQuery(document interface{}, m bson.M, mongoSession *mgo.Session, conn Conn) (err error) {
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
collection:= sessionCopy.DB(conn.Database).C(conn.Collection)
err = collection.Find(m).All(document)
if err != nil {
log.Printf("RunQuery : ERROR : %s\n", err)
}
return err
}
I will need to define many types: shoes != car.
<!-- language: «lang-go» -->
type Shoes struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Description string `bson:"descriprion"`
Size float `bson:"size"`
Color float `bson:"color"`
Type float `bson:"type"`
ShoeHeight float `bson:"shoeheight"`
PlatformHeight float `bson:"platformheight"`
Country float `bson:"country"`
}
type Car struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Model CarModel `bson:"name"`
Description string `bson:"descriprion"`
Color float `bson:"color"`
Height float `bson:"height"`
Fueltype string `bson:"fueltype"`
Country float `bson:"country"`
}
And my code will be copypaste:
<!-- language: «lang-go» -->
var carobjFindAll []Car
m := bson.M{"description": "description"}
_ = RunFindAllQuery(&carobjFindAll, m, mongoSession, conn)
for cur := range carobjFindAll {
fmt.Printf("\nId: %s\n", carobjFindAll[cur].Id)
fmt.Printf("\nColor: %s\n", carobjFindAll[cur].Color)
}
var shoesobjFindAll []Shoes
m_shoes := bson.M{"description": "shoes_description"}
_ = RunFindAllQuery(&shoesobjFindAll, m_shoes, mongoSession, conn)
for cur_shoe := range shoesobjFindAll {
fmt.Printf("\nId: %s\n", shoesobjFindAll[cur_shoe].Id)
fmt.Printf("\nColor: %s\n", shoesobjFindAll[cur_shoe].Color)
}
PS:
Sorry for my English
答案1
得分: 2
我不是MongoDB专家,但是我可以给出我的看法:
由于你有很多类别,你不需要为每个类型编写一个结构体,因为这既繁琐又不可靠。
相反,你应该直接使用bson.M
类型,它基本上是map[string]interface{}
类型的别名。
英文:
I'm not a MongoDB expert, but here is my take:
As you have plenty of categories, you don't have any itnerest in writing a struct for each of your types, as it would be both fastidious and unreliable.
Instead, you should work directly with the bson.M
type, which is basically an alias for the map[string]interface{}
type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论