如何编写包含许多类别(分类法>>100)的目录?

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

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(&quot;RunQuery : ERROR : %s\n&quot;, err)
}
   return err
}

I will need to define many types: shoes != car.
<!-- language: «lang-go» -->

type Shoes struct {
	Id             bson.ObjectId `bson:&quot;_id&quot;`
	Name           string        `bson:&quot;name&quot;`
	Description    string        `bson:&quot;descriprion&quot;`
	Size           float         `bson:&quot;size&quot;`
	Color          float         `bson:&quot;color&quot;`
	Type           float         `bson:&quot;type&quot;`
	ShoeHeight     float         `bson:&quot;shoeheight&quot;`
	PlatformHeight float         `bson:&quot;platformheight&quot;`
	Country        float         `bson:&quot;country&quot;`
}
type Car struct {
	Id          bson.ObjectId `bson:&quot;_id&quot;`
	Name        string        `bson:&quot;name&quot;`
	Model       CarModel      `bson:&quot;name&quot;`
	Description string        `bson:&quot;descriprion&quot;`
	Color       float         `bson:&quot;color&quot;`
	Height      float         `bson:&quot;height&quot;`
	Fueltype    string        `bson:&quot;fueltype&quot;`
	Country     float         `bson:&quot;country&quot;`
}

And my code will be copypaste:
<!-- language: «lang-go» -->

var carobjFindAll []Car
m := bson.M{&quot;description&quot;: &quot;description&quot;}
_ = RunFindAllQuery(&amp;carobjFindAll, m, mongoSession, conn)
for cur := range carobjFindAll {
	fmt.Printf(&quot;\nId: %s\n&quot;, carobjFindAll[cur].Id)
	fmt.Printf(&quot;\nColor: %s\n&quot;, carobjFindAll[cur].Color)
}
var shoesobjFindAll []Shoes
m_shoes := bson.M{&quot;description&quot;: &quot;shoes_description&quot;}
_ = RunFindAllQuery(&amp;shoesobjFindAll, m_shoes, mongoSession, conn)
for cur_shoe := range shoesobjFindAll {
	fmt.Printf(&quot;\nId: %s\n&quot;, shoesobjFindAll[cur_shoe].Id)
	fmt.Printf(&quot;\nColor: %s\n&quot;, 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.

huangapple
  • 本文由 发表于 2015年2月24日 15:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/28690722.html
匿名

发表评论

匿名网友

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

确定