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 (there are 100+ types)
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, because 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)
}
Maybe there is another way how to receive/update data in mongodb without writing thousand line of copypaste?
{
"name" : "Norberto Leite",
"title" : "Technical Evangelist",
"phone" : "0034.687.954.987",
"location" : ["Madrid, ES"],
"twitter" : ["@nleite"],
}
MongoDB World is back! June 1-2 in New York. Sponsorships and Call for Proposals now open!
$template
=
$twig
->loadTemplate(
'fruits.tmpl'
);
echo
$template
->render(
array
(
'fruitArray'
=>
$fruitArray,))
Twig template fruits.tmpl:
<div class="fruits"> {% for fruit in fruitArray %} {% if fruit.name|length %} <div>{{ fruit.name }}</div> {% endif %} {% if fruit.description|length %} <div>description: {{ fruit.description }}</div> {% endif %} {% endfor %} </div>
func ProcessCarsFind(document interface{}, m bson.M, mongoSession *mgo.Session, conn Conn) (err error) {
var shoesobjFindAll []Shoes
m_shoes := bson.M{"description": "shoes_description"}
_ = RunFindAllQuery(&shoesobjFindAll, m_shoes, mongoSession, conn)
for cur_shoe := range shoesobjFindAll {
fmt.Printf("\n
<div>Id: %s</div>\n", shoesobjFindAll[cur_shoe].Id) } }
--
You received this message because you are subscribed to a topic in the Google Groups "mgo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mgo-users/LfFIIjvtjBo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mgo-users+...@googlegroups.com.
Now what I see in go with code above:1.create special type2.write special functions for processing all result objects after querying to MongoDB.