Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write catalog with many categories(taxonomy >>100)?

Tags:

go

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:

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.

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:

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

like image 344
Valeriy Solovyov Avatar asked Feb 24 '15 07:02

Valeriy Solovyov


1 Answers

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.

like image 169
Elwinar Avatar answered Nov 02 '22 06:11

Elwinar